检查从sql表返回的数据是否包含linq中的小数列表值?

时间:2014-06-13 06:08:18

标签: c# asp.net linq

我想只从sql表中返回那些包含list中特定值的数据。我可以这样做吗?

  List<decimal> userHeirarchyId = new List<decimal>();
  List<decimal> userGroupId = new List<decimal>();

 //Get groups based on user      
 string userGroupIds = (from ucp in db.UserControlPrivileges
                        where ucp.UserId == (decimal)AMSECSessionData.userId
                        select ucp.GroupIds).FirstOrDefault().ToString();
 userGroupId = userGroupIds.Split(',').Select(x => decimal.Parse(x)).ToList();

 //Get heirarchy based on company and usergroup
 userHeirarchyId = db.Groups
                   .Where(x=>x.Hierarchy.CompanyId==(decimal)AMSECSessionData.companyId  
  =>stuck here      && x.GroupId.contains(userGroupIds ))

任何帮助将不胜感激..

2 个答案:

答案 0 :(得分:1)

试试这个。更改您使用Contains的顺序,例如“您的比较列表”。包含(会员名称)

var userHeirarchyId = db.Groups
                   .Where(x=> x.Hierarchy.CompanyId == (decimal)AMSECSessionData.companyId  
                   && userGroupIds.Contains(x.GroupId))

答案 1 :(得分:0)

像这样写:

var user = db.UserControlPrivileges.FirstOrDefault(u => 
                         u.UserId == (decimal)AMSECSessionData.userId);
if(user == null || user.GroupIds == null)
{
    return;
}
var userIds = user.GroupIds.Split(',').Select(x => decimal.Parse(x)).ToList();

然后:

var userHeirarchyId = db.Groups
                      .Where(x => x.Hierarchy.CompanyId==(decimal)AMSECSessionData.companyId  
                      && userIds.Contains(x => x.GroupId));