I have comma delimited list that looks like this:
string roleSearch = "Admin,Buyer,Broker";
And I have a List<Contacts>
that looks like this:
ContactID Roles
----------------------------
1 "Buyer,Partner"
2 "Broker"
3 "Admin,Buyer,Partner"
I am trying to write a query in Linq that will refine the List<Contacts>
to items that contain any of the roles in roleSearch
. So given:
roleSearch = "Admin,Buyer"
I want any record where Roles
contains "Admin" or "Buyer". Both are not necessary. If either "Admin" or "Buyer" are present, then it's a positive.
Using this post, I got close:
string[] inRoles = roleSearch .Split(',');
results = results.Where(r => inRoles.Contains(r.Roles));
But that's not it exactly, because it only returns record with an exact match of each item in inRoles
. Meaning, it only returns records with a single role. Any row where Roles
is something like "Admin,Partner" would be ignored.
I tried this:
string[] inRoles = roleSearch.Split(',');
results = results.Where(r => inRoles.Contains(r.Roles.Split(',')));
But that didn't work.
P.S. I can't count on the roles being in any order. BUT, good news, the list of potential roles is very small. Five max.