문제

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

도움이 되었습니까?

해결책

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()
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top