检查从sql表返回的数据是否包含linq中的十进制列表值
本文关键字:linq 十进制 列表 包含 数据 sql 返回 检查 是否 | 更新日期: 2023-09-27 18:08:38
我想只返回那些数据从sql表中包含来自列表的特定值。我该怎么做呢?
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 ))
试试这个。改变你使用Contains
的顺序,就像"你的比较列表"。包含(成员名称)
var userHeirarchyId = db.Groups
.Where(x=> x.Hierarchy.CompanyId == (decimal)AMSECSessionData.companyId
&& userGroupIds.Contains(x.GroupId))
这样写:
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));