使用另一个表的值搜索DataTable [英] Search DataTable with values from another table

查看:119
本文介绍了使用另一个表的值搜索DataTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用LINQ从另一个 DataTable 中搜索一个 DataTable ,但到目前为止还没有进展...如何做?



在下面的例子中,我有,其中我搜索, code> PlTable ,它只有一列;我需要从中检索每一行,其中 Name 字段至少包含一个 Dim ePlTable As IEnumerable(Of DataRow)= PlTable.AsEnumerable()

Dim found =从row在表中AsEnumerable
其中row(0).Contains(ePlTable)
选择行
返回found.CopyToDataTable.Rows

当然这不行,作为 .Contains String 作为参数

解决方案


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


这是非常的问题,所以使用强类型的 Field extension
方法将其转换为正确的类型, Enumerable.Any ,以查看此名称中是否包含至少一个字符串

  Dim strings =从行在PlTable中选择row.Field(Of String)(0)
Dim found =从row在表中AsEnumerable
其中strings.Any(Function(s)row.Field (Of String)(Name)。包含)
选择行
返回found.CopyToDataTable()


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()

这篇关于使用另一个表的值搜索DataTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆