I have the following Entity
public class Person
{
public int PersonId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
and have a list List<Person> badGuys
what I want to do is select from all persons except those in badGuys
List
My Code
context.Persons
.where(p => !badGuys.Contain(p))
.ToList()
but I get an error
Only primitive types or enumeration types are supported in this context.
How to fix this?
You can use the clause ALL with the distinct(!=)
var badBoys= from P in context.Persons
where badGuys.All(a => a.PersonId!= P.PersonId)
select P;
You could make an array containing the ids of the bad guys and filter out those ids (which are of a primitive type, so it should work):
var badGuyIds = badGuys.Select(x => x.PersonId).ToArray();
context.Persons
.Where(p => !badGuyIds.Contain(p.PersonId))
.ToList();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With