Search DataTable with values from another table

穿精又带淫゛_ 提交于 2019-12-11 05:19:36

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!