asp.net-mvc – LINQ to Entities无法识别方法异常

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – LINQ to Entities无法识别方法异常前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有这样的事情
SecuritySearcher sc = new SecuritySearcher();
Dictionary<string,bool> groupsMap = 
    sc.GetUserGroupMappings(domainName,currentUser,distGroups.ToList());

IQueryable<HotelTravel> groupq = 
    (from hotel in qHs
    join hp in qHps on hotel.HotelTravelId equals hp.HotelTravelId
    where !string.IsNullOrEmpty(hp.GroupName)
       && groupsMap.ContainsKey(hp.GroupName) 
       && groupsMap[hp.GroupName] == true
    select hotel);

在执行Linq语句时,它正在抛出异常说法
LINQ to Entities无法识别方法’Boolean ContainsKey(System.String)’方法,并且此方法无法转换为商店表达式.

解决方法

为了将表达式转换为数据库查询,数据库必须以某种方式知道字典的内容并有办法从查询中访问它. sql中没有字典机制,但这并不重要,因为您不需要字典,因为您只是在寻找值为某个常量的键.您可以将该组密钥转换为列表,并查看该列表是否包含您要查找的内容
var groupsList = (from kvp in groupsMap     // find all keys in groupsMap
                  where kvp.Value == true   // where the value is set to True
                  select kvp.Key).ToList();

IQueryable<HotelTravel> groupq =
    from hotel in qHs
    join hp in qHps on hotel.HotelTravelId equals hp.HotelTravelId
    where !string.IsNullOrEmpty(hp.GroupName)
          && groupsList.Contains(hp.GroupName)
    select hotel;

我怀疑你实际上并没有将空字符串作为字典中的键,这意味着你可以摆脱IsNullOrEmpty调用,只需要在groupsList.Contains(hp.GroupName)中.

原文链接:https://www.f2er.com/aspnet/245718.html

猜你在找的asp.Net相关文章