【问题标题】:Using Contains to do an IN statement within and LINQ clause using the Entity Framework使用 Contains 在使用实体框架的 LINQ 子句中执行 IN 语句
【发布时间】:2012-01-05 08:51:48
【问题描述】:

我正在尝试动态构建 LINQ where 语句,但在尝试使用列表中的 contains 方法编写 IN 语句时遇到问题。

这是我的代码:

IQueryable<Customer> customers =
                (from cus in objectCustomer
                 select cus); \\objectCustomer is all customers

//List of customer ID's passed into the method.
string[] sCustomerIDs = (string[])DictionaryParameters["CustomerIDs"];
customers = customers.Where(c => c.Assets.Any(a => sAssetIDs.Contains(a.UN_Asset.ToString())));

return customers.Cast<T>().ToList();

运行返回线时,我收到以下错误: LINQ to Entities 无法识别方法“System.String ToString()”方法,并且该方法无法转换为存储表达式。

有什么想法吗?如果我将 IQueryable 更改为 IEnumerable 以查询运行,但我需要使用 IQueryable,因为我正在使用实体框架,并且 IEnumerable 在我的过滤器发生之前返回所有记录。

非常感谢。

【问题讨论】:

    标签: linq c#-4.0 dynamic entity-framework-4 contains


    【解决方案1】:

    请试试这个:

    string[] sCustomerIDs = (string[])DictionaryParameters["CustomerIDs"];    
    
    IQueryable<Customer> customers =   
                (from cus in objectCustomer  
                 where sCustomerIDs.Contains(cus.*[Your Field]*)
                 select cus);   
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-01
      • 2013-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多