【问题标题】:LINQ query for WHERE IN clasueWHERE IN 子句的 LINQ 查询
【发布时间】:2017-07-13 06:56:01
【问题描述】:

我有 4 张桌子 TransactionsAttachmentSubReportTypeSubRepRole。我的缓存上有用户角色。我想要那些与用户角色相关的附件[一个用户可能有多个角色]

List<int> userrole = new List<int>();
UserContext user_details = (UserContext)HttpContext.Current.Cache["UserContext"];
IList<UserRole> userrole_id = user_details.UserRoleModuleWise;

var  query = (from trans in objContext.Transactions
             join attch in objContext.Attachment on trans.TransId equals attch.TransId
             join subrept in objContext.SubReportType on trans.SubRepId equals subrept.SubRepId
             join subreprl in objContext.SubRepRole on trans.SubRepId equals subreprl.SubRepId
             join selectedrole in userrole_id  on subreprl.RoleId equals selectedrole.RoleId
             /*where obj.Contains(subreprl.RoleId) */orderby trans.TransDate
           select new AttachmentModel
           {
               Createdate = attch.CreatedDateTime,
               FileType = attch.FileType,
               FileName = attch.FileName,
               Attachid = attch.AttachedId,
               FileTag = attch.FileTag,
               Transid = trans.TransId,
               SubReportName = subrept.SubRepName,
               RandomPinNo = attch.FileRandomPin
           }).ToList();

现在出现这个错误:

无法创建类型的常量值 'User.Common.DataContract.UserRole'。只有原始类型或 在此上下文中支持枚举类型。

请帮忙。也尝试过“包含”,但类型转换错误即将到来。只是想拥有那些角色在 user_details.UserRoleModuleWise 中的记录。 user_details.UserRoleModuleWise 是一个具有 RoleId 和 RoleName 的数组

【问题讨论】:

  • 你想检查什么 /*where obj.Contains(subreprl.RoleId) *.

标签: linq where


【解决方案1】:

您需要将Contains 与正确值的数组一起使用:

where user_details.UserRoleModuleWise.Select(urmw => urmw.RoleId).ToArray().Contains(aRoleId => aRoleId == subreprl.RoleId)

【讨论】:

    【解决方案2】:

    创建了一个通用类——

     object[] common_data = new object[4];
         UserContext user_details = (UserContext)HttpContext.Current.Cache["UserContext"];
            List<UserRole> userrole_id = user_details.UserRoleModuleWise.ToList();
           int[] roleid = { -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -12, -13, -14, -15, -16, -17, -18, -19, -20 };
            string[] rolename = { "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" };
            for (int i = 0; i < userrole_id.Count; i++)
                {
                    roleid[i] = Convert.ToInt32(userrole_id[i].RoleId);
                    rolename[i] = Convert.ToString(userrole_id[i].RoleName);
                    r_id.Add(roleid[i], rolename[i]);
                }
                common_data[0] = roleid; 
                common_data[1] = rolename; 
               //common_data[2] = username; 
               common_data[3] = userrole_id.Count;
    

    然后在我的存储库中我使用了 -

            User_common_data ucd = new User_common_data();
            object[] ucd_data = ucd.user_common_details();
            int[] roles = (int[]) ucd_data[0];
    
            var query = (from obj in objContext.ReportType
                         join obj1 in objContext.SubReportType on obj.RepId equals obj1.RepId
                         join obj2 in objContext.SubRepRole on obj1.SubRepId equals obj2.SubRepId
                         where roles.Contains(obj2.RoleId)
                         select new ReportTypeModel
                         {
                             RepId = obj.RepId,
                             RepName = obj.RepName,
                             RepCode = obj.RepCode
                         }).ToList();
    

    现在它工作正常。 @NetMage 的回答也有效...谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-31
      • 1970-01-01
      • 2014-05-02
      • 2020-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多