gpt4 book ai didi

c# - Entity Framework 中的 "Not In"

转载 作者:太空狗 更新时间:2023-10-30 00:01:20 25 4
gpt4 key购买 nike

我有以下实体

public class Person
{
public int PersonId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}

并有一个列表List<Person> badGuys

我想做的是从除badGuys 之外的所有人中进行选择列表

我的代码

context.Persons
.where(p => !badGuys.Contain(p))
.ToList()

但是我得到一个错误

Only primitive types or enumeration types are supported in this context.

如何解决这个问题?

最佳答案

您可以创建一个包含坏人 ID 的数组并过滤掉这些 ID(它们是原始类型,因此它应该可以工作):

var badGuyIds = badGuys.Select(x => x.PersonId).ToArray();

context.Persons
.Where(p => !badGuyIds.Contain(p.PersonId))
.ToList();

关于c# - Entity Framework 中的 "Not In",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33668337/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com