"Accessing the member" DataType MemberName "from" Namespace.ClassName "is not legal by type" System.Collections.Generic.IEnumerable`1 [Namespace.ClassName] ".

I would love the solution to my current problem, but I would like EVEN MORE if I can understand what this error actually means.

I have LINQ to SQL classes for two tables in my database: Providers and Destinations . 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 the GridView using a query that is retrieved from the parent Provider and uses the Destination child, for example:

 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(); } 

In .DataBind (), when it does run the query, it throws the following error:

Access to the 'System.String Category' member from 'Namespace.Assignment' is not legal by type 'System.Collections.Generic.IEnumerable`1 [Namespace.Assignment].

I tried checking for zeros (a => a! = Null && a.Category ...), but that didn't work. I'm not sure what to try next. I think that if I knew that the error was trying to tell me, I could find a solution. Be that as it may, I do not know why membership would be illegal.

+4
source share
3 answers

This Assignments property is incorrect. First of all, getters properties should not have side effects, and more importantly, object classes should never have inverse dependencies on a DataContext . Linq to SQL cannot decrypt this query; he relies on a property that does all kinds of crazy things that Linq to SQL cannot hope to understand.

Now get rid of this Assignments property. Instead, you need to write this query as a connection:

 int category = (int)ddlCategory.SelectedValue; var providers = from p in context.Providers join a in context.Assignments on p.ProviderID equals a.ProviderID into g where g.Count(ga => ga.Category == category) > 0 select p; 

This should do what you are trying to do if I correctly understood the intent of your code.

One final note: you never properly place a DataContext in any of your methods. You should wrap it like this:

 using (var context = new linqDataContext()) { // Get the data here } 
+3
source

I think somewhere he doesn't know this type, which is in IEnumerable. You are trying to call a method that is not part of IEnumerable inteface.

Why don't you just move the request from the property to the PopulateProviders () method?

0
source

Remove your specified Assignments property. In the Linq-To-SQL dbml file, create a relationship between Providers and Destinations, and Providers as the parent property and ProviderID as the participation property for both objects. LINQ will generate the "IEnumerable Assignments" property based on matches between the ProviderID using a consistent DataContext.

0
source

Source: https://habr.com/ru/post/1301630/


All Articles