标识错误:LINQ to 实体无法识别方法'Boolean get_Item(System.String)'方法,



我的WCF实体框架代码中有这行代码

if (criteria.AccommodationTypes != null && criteria.AccommodationTypes.Count > 0)
   result = result.Where(a => criteria.AccommodationTypes[a.Type]);

这就是执行时导致此错误的原因

LINQ to Entities does not recognize the method 'Boolean get_Item(System.String)' method, and this method cannot be translated into a store expression.

AccommodationTypes是一个IDictionary。我可以看到问题是EF无法将我的代码转换为SQL,这就是它失败的原因,但我看不出需要编写什么查询才能实现此功能。

谢谢,

Sachin

对于将来有类似问题的人,以下是我解决问题的方法。我在字典中创建了一个我感兴趣的所有值的列表(对应布尔值为true的值),然后在该列表的查询中执行包含。

var types = (from type in criteria.AccommodationTypes where type.Value select type.Key).ToList();
if (criteria.AccommodationTypes != null && criteria.AccommodationTypes.Count > 0)
  result = result.Where(a => types.Contains(a.Type));

相关内容

最新更新