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到实体不支持引用非标量变量(Person
)进行过滤。Linq到EF:如何使用非原始类型
我该如何解决这个问题?谢谢!
//编辑:我试图从personEntities中找到person,与personOfInterest列表中的任何人都有相同的名称和相同的工作。例如,我试图在我的人物中找到任何人,他是名叫鲍勃的警察或名叫约翰的程序员。
我得到的错误描述于here。(22.2)
什么是异常的信息?是关于“Linq to Entities不支持引用非标量变量(Person)”的部分。“从你的分析的消息来看?因为我相信这是因为“poi.Job == p.Job”。 – AbdouMoumen 2011-04-20 12:32:26
似乎你的代码不会编译。 'Contains'将'Person'对象作为第一个参数,而不是lambda表达式。你应该使用'Any'来代替。 – Yakimych 2011-04-20 13:36:30
你能用文字描述你到底在做什么和正在做什么? – Andrei 2011-04-20 23:47:21