Beyto
Beyto

Reputation: 379

Linq match string list and string value

I used below code

 string userList = string.Join(",", db.UserRole.Where(p => p.FirmUserID == 1).Select(p => p.RoleID.ToString()));

userList values below,

1,12,17,33,76

I have also below string list

List<string> roleList = new List<string>();
roleList.add("14");
rolelist.add("33");
roleList.add("76");

How can i check userList includes roleList value and get matches value in entity framework ?

Upvotes: 0

Views: 547

Answers (1)

user3021830
user3021830

Reputation: 2924

First of all instead of converting your userList to string keep it as a List

List<string> userList = db.UserRole.Where(p => p.FirmUserID == 1).Select(p => p.RoleID.ToString()).Tolist();

To check if userList includes roleList

bool doesInclude = !roleList .Except(userList).Any();

to get the matches:

List<string> matches = userList.Intersect(roleList).ToList();

But if this is what you need to accomplish you'd better work sith integer types instead of string, because string comparison is more error prone (by the user).

Upvotes: 2

Related Questions