I would love a solution to my current problem, but I would love EVEN MORE if I can understand what that error actually means.
I have LINQ to SQL classes for two tables in my DB: Providers and Assignments. I added the following member to the Provider class:
public IEnumerable<Assignment> Assignments
{
get
{
return (new linqDataContext())
.Assignments
.Where(a => a.ProviderID == this.ProviderID);
}
}
Then, I bind a GridView using a query that pulls from the parent Provider and uses the child member Assignments, like this:
protected void PopulateProviders()
{
linqDataContext context = new linqDataContext();
var list = from p in context.Providers
where (p.Assignments.Count(a => a.Category == ddlCategory.SelectedValue) > 0)
select p;
lvProviders.DataSource = list;
lvProviders.DataBind();
}
At .DataBind(), when it actually runs the query, it throws the following error:
Member access ‘System.String Category’ of ‘Namespace.Assignment’ not legal on type ‘System.Collections.Generic.IEnumerable`1[Namespace.Assignment].
I’ve tried checking for nulls (a => a != null && a.Category …) but that hasn’t worked. I’m not sure what to try next. I think that, if I knew what the error is trying to tell me, I could find the solution. As it stands, I don’t know why member access would be illegal.
That
Assignmentsproperty is all wrong. First of all, property getters should not have side-effects, and more importantly, entity classes should never have reverse dependencies on theDataContext. Linq to SQL has no way to decipher this query; it’s relying on a property that does all sorts of crazy stuff that Linq to SQL can’t hope to understand.Get rid of that
Assignmentsproperty now. Instead of doing that, you need to write this query as a join:That should do what you’re trying to do if I understood the intent of your code correctly.
One last side note: You never dispose properly of the
DataContextin any of your methods. You should wrap it like so: