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:
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));