Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

System.InvalidOperationException: 'The LINQ expression could not be translated. Either rewrite the query in a form that can be translated

I am trying to get all pens when are not blue by using below
expression. there are two lists pens and bluepens…

var pensToDelete = _repository.GetPens().Where(x => bluePens.All(y => y.Id != x.Id));

when I am accessing pensToDelete as below

if (pensToDelete .Count() > 0)
{
}

I get the below error:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

System.InvalidOperationException: ‘The LINQ expression ‘DbSet()
.Where(a => __bluePens_0
.All(y => y.Id != a.Id))’ could not be translated. Either rewrite the query in a form that can be translated, or switch to
client evaluation explicitly by inserting a call to ‘AsEnumerable’,
‘AsAsyncEnumerable’, ‘ToList’, or ‘ToListAsync’.

Not sure how to fix this

>Solution :

As we discussed in comments be careful when you want using ToList(), always try to use it after your main query because it is costly and retrieve all the records before anything something like Where().Tolist() not ToList().Where()
How about this:

var pensToDelete = _repository.GetPens()
                              .Where(x => !bluePens.Select(s => s.Id).ToArray().Contains(x.Id));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading