2009-11-16 75 views
4

当通过实体框架查询数据库时,是否有排除对象属性值列表的方法?实体框架 - 排除值列表

我试图滑头拉这个数字:

List<String> StringList = new List<String>(); 
StringList.Add("ya_mama"); 
StringList.Add("has"); 
StringList.Add("fleas"); 

servicesEntities context = new servicesEntities(); 
var NoFleasQuery = (from x in context.person 
        where !StringList.Any(y => y.CompareTo(x.the_string_I_dont_want_it_to_be) == 0) // <--- the part where I thought I was slick 
        select x); 

它编译,但之后,我跑了它,它给了我这个错误:

Unable to create a constant value of type 'Closure type'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.

“闭合型”?我的关闭如何!实体框架......你伤了我的心。

回答

3

什么步骤呢?

var NoFleasQuery = (from x in context.person 
         where !StringList.Any(y => x.the_string_I_dont_want_it_to_be == y) 
         select x); 

它适用于我。但是生成的SQL相当糟糕:

SELECT 
1 AS [C1], 
[Extent1].[Id] AS [Id], 
[Extent1].[Name] AS [Name], 
[Extent1].[Address_Id] AS [Address_Id] 
FROM [dbo].[Person] AS [Extent1] 
WHERE NOT EXISTS (SELECT 
    1 AS [C1] 
    FROM (SELECT 
     N'John Doe' AS [C1] 
     FROM (SELECT 1 AS X) AS [SingleRowTable1] 
    UNION ALL 
     SELECT 
     N'Jack Black' AS [C1] 
     FROM (SELECT 1 AS X) AS [SingleRowTable2]) AS [UnionAll1] 
    WHERE [Extent1].[Name] = [UnionAll1].[C1] 
) 

它建立一个子查询这是其他子查询的联合,而不是使用“NOT IN” ......也许不是很有效!

0

试试这个:

where !StringList.Contains(x.the_string_I_dont_want_it_to_be) 
+0

它给了我这个数字: LINQ to Entities不识别方法'Boolean Contains(System.String)'方法,并且这个方法不能被转换成存储表达式。 – 2009-11-16 09:27:52