vb.net 使用另一个表中的值搜索 DataTable

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15005448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:27:24  来源:igfitidea点击:

Search DataTable with values from another table

vb.netlinqdatatablelinq-to-dataset

提问by utrack

I'm trying to search one DataTablewith values from anotherDataTableusing LINQ, but no progress so far... How to do it?

我正在尝试使用 LINQDataTable从另一个值中搜索一个值DataTable,但到目前为止没有进展......怎么做?

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 Namefield contains at least one string of Namefield in PlTable's rows.

在下面的示例中,我有table,我在其中搜索,而PlTable,只有一列;并且我需要从 中检索每一行table,其中该字段在的行Name中至少包含一个Name字段字符串PlTable

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 .Containswants Stringas argument

当然它不起作用,就像.Contains想要的String一样

采纳答案by Tim Schmelter

Surely it does not work, as .Contains wants String as argument

当然它不起作用,因为 .Contains 想要 String 作为参数

That's exatly the problem, so use the strongly typed Fieldextension method to cast it to it's correct type and Enumerable.Anyto look if at least one string is contained in this Name:

这正是问题所在,因此请使用强类型Field扩展方法将其强制转换为正确的类型,并Enumerable.Any查看是否至少包含一个字符串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()