how to search between comma separated field in linq
How to search for a value which is comma separated in database through linq. My scenario is user can select Multiple value from listbox and then search those item in comma separated field in database. I select two items so the value will be 2,3 and database field have values
1,2,4,5
1,4,3,6
2,3,4,5
1,4
The selected records must be record no 1 because it has 2, record no 2 because it has 3 and record no 3 because it has both and reject record n开发者_运维知识库o 4. What i try is
string Commodites = "2,3";
obj.Where(e => Commodites.Contains(e.Id)).Distinct()
but it select only those record which have value 2,3 only
For CSV splitting I advise you to use any CSV parser, not simply use string.Split
method.
string[] input = { "1,2,4,5", "1,4,3,6", "2,3,4,5", "1,4" };
var result = input.Where(l => l.Split(',').Any(s => new[] { "2", "3" }.Contains(s)));
精彩评论