Moh*_*adr 4 c# linq entity-framework
我有以下实体
public class Person
{
public int PersonId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
并有一个清单 List<Person> badGuys
我想要做的是从badGuys
列表中除了那些人之外的所有人中选择
我的守则
context.Persons
.where(p => !badGuys.Contain(p))
.ToList()
Run Code Online (Sandbox Code Playgroud)
但是我收到了一个错误
在此上下文中仅支持基元类型或枚举类型.
如何解决这个问题?
Cri*_*scu 16
你可以创建一个包含坏人的ID的数组并过滤掉那些id(它们是原始类型,所以它应该工作):
var badGuyIds = badGuys.Select(x => x.PersonId).ToArray();
context.Persons
.Where(p => !badGuyIds.Contain(p.PersonId))
.ToList();
Run Code Online (Sandbox Code Playgroud)
小智 5
您可以将子句 ALL 与 unique(!=) 一起使用
var badBoys= from P in context.Persons
where badGuys.All(a => a.PersonId!= P.PersonId)
select P;
Run Code Online (Sandbox Code Playgroud)