Reputation: 567
I want to return only those data from sql table which contains specific values from list.How can i do that ?
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 ))
Any help will be appreciated..
Upvotes: 0
Views: 744
Reputation: 2178
Write it like this :
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();
And then :
var userHeirarchyId = db.Groups
.Where(x => x.Hierarchy.CompanyId==(decimal)AMSECSessionData.companyId
&& userIds.Contains(x => x.GroupId));
Upvotes: 0
Reputation: 3237
Try this. Change the order how you're using Contains
like 'your compare list'.Contains(member name)
var userHeirarchyId = db.Groups
.Where(x=> x.Hierarchy.CompanyId == (decimal)AMSECSessionData.companyId
&& userGroupIds.Contains(x.GroupId))
Upvotes: 1