LINQ to Entities无法识别方法'布尔HasFlag(System.Enum)'方法,并且此方法无法转换为商店表达式

时间:2015-07-27 13:07:17

标签: linq

我有这项服务:

//seroiunoiweucroewr
///wercewrwerwerwer
//wcererewrwerwer
public List<UserRoleContract> GetRolePagesByUserId(long plngUserId, DisplayType displayType)
{            
    List<UserRoleContract> result = new List<UserRoleContract>();            
    using (CitiCallEntities context = new CitiCallEntities())
    {
        try
        {
            //var DisplayList = Utility.GetEnumDescriptions(typeof(DisplayType)).ToList();

            //var selectValue = DisplayList.Where(i => i.Key == (byte) DisplayType.Windows).FirstOrDefault();

            result = (from oUser in context.User

                      join oUserRole in context.UserRole on oUser.Id equals oUserRole.UserId

                      join oRoleRightsPage in context.RoleRightsPage.Where(i => i.IsActive == true) 
                      on oUserRole.RoleId equals oRoleRightsPage.RoleId
                      join oApplicationPage in context.ApplicationPage.Where(i => i.IsActive == true) 
                      on oRoleRightsPage.PageId equals oApplicationPage.Id

                      join oRole in context.Role on oUserRole.RoleId equals oRole.Id

                      join oEmployee in context.Employee on oUser.EmployeeId equals oEmployee.Id
                      join oSection in context.Section on oEmployee.SectionId equals oSection.Id
                      where oUser.IsActive == true && oUser.Id == plngUserId
                      && oRole.IsActive == true && (((DisplayType)oRoleRightsPage.DisplayType).HasFlag(displayType))

//am getting error in has flag 
// am having three display type web, windows and all 
// how to overcome
                      select new UserRoleContract
                      {
                          UserId = oUser.Id,
                          RoleId = oRole.Id,
                          RoleName = oRole.RoleName,
                          PageID = oApplicationPage.Id,
                          PageName = oApplicationPage.PageName,
                          IsOPsCtrl = oRole.IsOPsCtrl,
                          ISOPsCtrlFor = oRole.OPsCtrlFor,
                          SectionId = oSection.Id,
                          DisplayType = oRoleRightsPage.DisplayType,
                      }).Distinct().ToList();
        }
        catch (Exception exception)
        {
            HandleExpcetion(exception);
            //throw new CitiCallException(exception.Message);
        }
    }
    return result;
}

我在有标志转换时遇到Linq错误,我该如何克服这个问题?

2 个答案:

答案 0 :(得分:0)

您是geeting错误,因为HasFlag方法在数据库中不是paresent,即它可能是代码中语言或本地函数的一部分,而这些函数在数据库中不存在。

因此,当查询被翻译时,它发现此方法不可用,这就是您收到错误的原因。

避免此错误的一种解决方案是

  1. 从数据库中提取所有数据

  2. 比过滤数据,即应用它的HasFlag方法。

  3. 但这会带来所有数据并可能会降低性能。

    示例是

    从查询中删除此行(((DisplayType)oRoleRightsPage.DisplayType).HasFlag(displayType)

    var list = querieddata //first fetch data without hasflag condition/method 
                       .AsEnumerable() // Rest of the query in-process
                       .Where(oRoleRightsPage=> ((DisplayType)oRoleRightsPage.DisplayType).HasFlag(displayType))//apply condition here once fetching done
                       .ToList();
    

答案 1 :(得分:0)

HasFlag方法在Linq to Entities中没有等效项,这就是您收到该错误的原因。您可以使用按位比较而不是使用HasFlag来解决它,例如:

((DisplayType)oRoleRightsPage.DisplayType).HasFlag(displayType)

变为:

(oRoleRightsPage.DisplayType & displayType) > 0