Home > Net >  LINQ Extract partial match from dataTable from list
LINQ Extract partial match from dataTable from list

Time:05-21

I'm trying to extract all rows from a dataTable that for column "A" have at the end a string from a list.

For example:

DataTable

ID     A
1      1220
2      1221
3      1223

list

{"20","23"}

To return 1 1220 and 3 1223

(From x In dt.AsEnumerable() 
Where list.Any(Function(l) l.EndsWith(x("number").ToString))
Select x).CopyToDataTable

This is what I've come up so far but it only extracts a row if there is a full match in the list, say 12220, for 20 it doesn't return anything.

Can you help ?

CodePudding user response:

You tagged your question with C#. In C# the query would be

dataTable
    .Where(r => list.Any(s => r.A.EndsWith(s)))

The test code I tried is

var array = dataSet1
    .DataTable1
    .Where(r => list.Any(s => r.A.EndsWith(s)))
    .ToArray();

Where dataSet1 is a strongly typed dataset and DataTable1 is a table with columns Id and A

  • Related