T>从DataSet使用LINQ,其中RowsID的名单是在一个名单,其中选择行; [英] Select Rows from a DataSet using LINQ, where the list of RowsID's are in a List<T>

查看:135
本文介绍了T>从DataSet使用LINQ,其中RowsID的名单是在一个名单,其中选择行;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我必须说,我是使用LINQ一个纽比。其实我从来没有使用过,但我有一个任务,我需要过滤数据表中,使用值将来自一个列表。 所以,我会想知道是否有可能在LINQ到列表中的过滤器值数据表中的值进行查询。有些人可以给我一些提示的。

First I have to say, that I am a newby using LINQ. Actually I never used before, but I am having a task where I need to filter a DataTable, using values that will come from a List. So I will like to know if it's possible in LINQ to query on a Datatable using values in the List as Filter values. Some one can give me some hint's

感谢你。

推荐答案

要做到这一点,最好的方法取决于你打算做什么用的过滤效果。您是否需要将结果反馈的数据表进行进一步的操作,或者是你数据绑定的结果?

The best way to do this depends on what you plan to do with the filtered results. Do you need the results back as DataTable for further operations, or are you databinding to the results?

以下面的例子中,它返回(绑定)匹配的DataRows枚举

Take the example below, which returns a (bindable) enumerator of matching DataRows

//create sample table with sample rows
DataTable table = new DataTable();

table.Columns.Add("id", typeof(int));

for (int i = 1; i < 11; i++)
{
    DataRow row = table.NewRow();
    row[0] = i;
    table.Rows.Add(row);
}

//filter the table by id (in a list)
List<int> rowIds = new List<int> { 1, 3, 6 };

IEnumerable<DataRow> matchingRows = from DataRow row in table.Rows
                   where rowIds.Contains((int)row[0])
                   select row;

如果你需要一个数据表,你可以导入行到另一个表:

If you need a DataTable you could import the rows into another table:

DataTable filteredTable = table.Clone();

foreach (DataRow filteredRow in matchingRows)
{
    filteredTable.ImportRow(filteredRow);
}

这篇关于T&GT;从DataSet使用LINQ,其中RowsID的名单是在一个名单,其中选择行;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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