There is the following linq request

 var findInfoKey = dsCr.Items.Where(keyItem => keyItem.CR_ReportID == idProject); 

I want to understand how many records returned. Tried to learn through the Count() method, but an exception is thrown.

LINQ to Entities doesn’t recognize the System.Xml.Linq.XElement Element (System.Xml.Linq.XName) method, this method cannot be recognized.

I do not understand what the problem is. Please help me figure it out.

Entity Framework version: 6

*** I apologize, I actually did not write the full request, considering that it was not important, in fact the request looked like this:

 var findInfoKey = dsCr.Items.Where(keyItem => keyItem.CR_ReportID == idProject && keyItem.iv_ID == _item.Element("iv_ID").Value); 

The whole problem was in _item.Element ("iv_ID"). Value, I understood that these requests are not compatible, i.e. LinqToEntity is used in LinqToObjects request, as if I understood correctly. I would be grateful if someone explains this error.

  • var findInfoKey = dsCr.Items.Where(keyItem => keyItem.CR_ReportID == idProject).ToList(); and then findInfoKey.Count() - Sublihim
  • Pull out the value of _item.Element("iv_ID").Value before the linq query itself. Then insert a variable with this value into the request. - Monk

2 answers 2

When you call the Queryable.Where method, no records are yet "returned." The Where method only builds the request; to execute the request, you must call the materializing method.

For example, through Enumerable.ToList . Your option - calling Queryable.Count also executes the query, but not entirely - but first changes it so that it returns only the number of records.

Therefore, what you have mistaken for an error of the Count method is in fact an error in the query. Specifically, in this case, the EF library did not understand that the expression _item.Element("iv_ID").Value can be calculated locally and tried to convert it to SQL. Unsuccessfully.

Take out the value of the XML element in a separate variable and compare it with it already. The error should be gone.

    Extract the content in the List and count

     using (MyContext context = new MyContext()) { Document DocObject = context.Document.Find(_id); int GroupCount = context.Document.Where(w=>w.Group == DocObject.Group).ToList().Count(); } 

    Direct request to the database without downloading all the content:

     using (MyContext context = new MyContext()) { Document DocObject = context.Document.Find(_id); int GroupCount = context.Document.Where(w=>w.Group == DocObject.Group).Count(); } 
    • context.Document.Count(w=>w.Group == DocObject.Group); - tCode
    • @tCode, syntactic sugar ... - iluxa1810