问题
I'm trying to search one DataTable
with values from anotherDataTable
using LINQ, but no progress so far... How to do it?
In example below i have table
, in which i search, and PlTable
, which has only one column; and i need to retrieve every row from table
, in which the Name
field contains at least one string of Name
field in PlTable
's rows.
Dim ePlTable As IEnumerable(Of DataRow) = PlTable.AsEnumerable()
Dim found = From row In table.AsEnumerable
Where row(0).Contains(ePlTable)
Select row
Return found.CopyToDataTable.Rows
Surely it does not work, as .Contains
wants String
as argument
回答1:
Surely it does not work, as .Contains wants String as argument
That's exatly the problem, so use the strongly typed Field
extension
method to cast it to it's correct type and Enumerable.Any
to look if at least one string is contained in this Name
:
Dim strings = From row In PlTable Select row.Field(Of String)(0)
Dim found = From row In table.AsEnumerable
Where strings.Any(Function(s) row.Field(Of String)("Name").Contains(s))
Select row
Return found.CopyToDataTable()
来源:https://stackoverflow.com/questions/15005448/search-datatable-with-values-from-another-table