Linq To EF:如何使用非原始类型进行过滤

发布于 2024-11-02 14:36:24 字数 736 浏览 0 评论 0原文

public class Person
{
   public int ID { get; set; }
   public int Job { get; set; }
   public string Name { get; set; }
}

List<Person> personsOfInterest = GetPersonsOfInterest();

PersonEntities personEntities = new PersonEntities();

var filteredPersons = personEntities.Where(p => personsOfInterest.Any(poi => poi.Job == p.Job && poi.Name == p.Name));

上面的代码生成 NotSupportedException,因为 Linq to Entities 不支持引用非标量变量 (Person)。

我该如何解决这个问题?谢谢!

//编辑:我试图从 personEntities 中查找人员,他们与 personOfInterest 列表中的任何人具有相同的姓名和相同的工作。例如,我试图在我的实体中找到一个名为 Bob 的警察或名为 John 的程序员。
我收到的错误在此处中进行了描述。(22.2)

public class Person
{
   public int ID { get; set; }
   public int Job { get; set; }
   public string Name { get; set; }
}

List<Person> personsOfInterest = GetPersonsOfInterest();

PersonEntities personEntities = new PersonEntities();

var filteredPersons = personEntities.Where(p => personsOfInterest.Any(poi => poi.Job == p.Job && poi.Name == p.Name));

The above code generates a NotSupportedException, because Linq to Entities does not support referencing non-scalar variables(Person).

how can I resolve this? thanks!

//edit: I am trying to find persons from personEntities, who has same name and same job with anyone in the personOfInterest list. for example, I am trying to find anyone in my personEntities who is a Policeman named Bob or Programmer named John.
the error I'm getting is described in here.(22.2)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

流殇 2024-11-09 14:36:24

首先,两个集合应该包含相同类型的对象。

然后你可以执行以下操作:

    var filteredPerosns = personEntities
          .Where(p => personsOfInterest.Contains(p, new MyPersonComparer()));

创建类:

    class MyPersonComparer : IEqualityComparer<Person>
    {
        public bool Equals(Person x, Person y)
        {
            return x.Job == y.Job && x.Name == y.Name; 
        }

        public int GetHashCode(Person obj)
        {
            return obj.PersonID; //Just for example...
        }
    }

OR如果第一个不是一个选项,你可以按照以下方式进行连接(概念上):

    List<int?> listA = new List<int?>() {1, 2, 3, 4, 5, 6, 7};
    List<int?> listB = new List<int?>() {5};

    bool result = (from a in listA
                   join b in listB on a equals b 
                   select a).Any();

我不知道内部情况您的类,因此您必须调整示例以适应您的对象结构。

编辑:
我修改了上面的示例以反映您编辑的描述:

    List<Person> personsOfInterest = GetPersonsOfInterest();

    var filteredPersons = (from a in personEntities
           join b in personsOfInterest on new{a.Name, a.Job} equals new {b.Name, b.Job} 
           select a).ToList();

代码中的 PersonEntities,是自定义集合类型还是 EF 模型中的表/复杂类型?

First of all both collections should contain objects of the same type.

Then you could do the following:

    var filteredPerosns = personEntities
          .Where(p => personsOfInterest.Contains(p, new MyPersonComparer()));

create the class:

    class MyPersonComparer : IEqualityComparer<Person>
    {
        public bool Equals(Person x, Person y)
        {
            return x.Job == y.Job && x.Name == y.Name; 
        }

        public int GetHashCode(Person obj)
        {
            return obj.PersonID; //Just for example...
        }
    }

OR if the first is not an option, you could do a join something along the lines(in concept):

    List<int?> listA = new List<int?>() {1, 2, 3, 4, 5, 6, 7};
    List<int?> listB = new List<int?>() {5};

    bool result = (from a in listA
                   join b in listB on a equals b 
                   select a).Any();

I do not know the insides of your classes, so you will have to adjust the examples to fit your object structure.

EDITED:
I have modified the above example to reflect your edited description:

    List<Person> personsOfInterest = GetPersonsOfInterest();

    var filteredPersons = (from a in personEntities
           join b in personsOfInterest on new{a.Name, a.Job} equals new {b.Name, b.Job} 
           select a).ToList();

PersonEntities in your code, is that a custom collection type or is this a table/complex type in your EF model?

池予 2024-11-09 14:36:24

最好比较 ID 而不是对象。这样效率会高很多。问题是 EntityFramework 不知道如何将 obj1 == obj2 转换为 SQL。

It would best to compare the IDs rather than the objects. This will be much more efficient. The problem is EntityFramework does not how to translate obj1 == obj2 into SQL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文