0

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..

0

2 Answers 2

1

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))
Sign up to request clarification or add additional context in comments.

Comments

0

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));

2 Comments

what if db.UserControlPrivileges.FirstOrDefault(u => u.UserId == (decimal)AMSECSessionData.userId) returns null?
@NedStoyanov, thanks for your input, but that is common practice, so i didn't feel it necessary to write null check and all that. Just showing the logic. but I am updating it then.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.