Reputation: 165
I have a database with a column can contain value separated by commas, as:
madrid, barcelona, paris
rome
paris
amsterdam, madrid
florence, tokyo
paris, barcelona
milan
london, edinburgh
....
Then I have a List as:
var listTarget = new List<string>{"paris", "amsterdam", "rome"};
In LINQ I want extract all rows that contain at least a value in listTarget, so the result will be:
madrid, barcelona, paris
rome
paris
amsterdam, madrid
Anyone can help me?
Upvotes: 0
Views: 52
Reputation: 33
Excuse my vb.net linq but you can convert it to C#. What I have used is a combination of linq and regex to get the accurate result check out this code
DT.AsEnumerable().Where(Function(n) system.Text.RegularExpressions.Regex.IsMatch((cstr(n("A"))),string.Join("|",List_of_strr) )).CopyToDataTable
And I also used a datatable here as I didn't have a database. The "A"
in cstr(n("A")
is the column name.
Upvotes: 0
Reputation: 18155
You could split using delimiter (in this case ',') and compare the lists. For example,
.Where(x=> x.City.Split(',').Any(c=> listTarget.Contains(c.Trim())));
Upvotes: 1