从一个数组C#与过滤值的数据表 [英] filter a data table with values from an array c#

查看:246
本文介绍了从一个数组C#与过滤值的数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个delete方法,它得到的GUID的数组,我有一个数据表...我怎样才能筛选数据表,因此将只包含thoes的GUID?

I have a delete method, it gets an array of GUIDs and I have a data table... how can I filter the data table so it will only contain thoes GUIDs?

public void delete(Guid[] guidlist)
{
datatable template = ReadTemplateList()
...
}

感谢您!

推荐答案

使用LINQ到数据集,你可以创建新的数据表这将只与那些的GUID行:

With Linq to DataSet you can create new DataTable which will have only rows with those Guids:

public void Delete(Guid[] guids)
{
   DataTable table = ReadTemplateList()
                         .AsEnumerable()
                         .Where(r => guids.Contains(r.Field<Guid>("ColumnName")))
                         .CopyToDataTable();

   // ...
}

另一种选择(这也将在旧版本的.NET工作)与支持的的过滤器内置的RowFilter功能过滤你的表。让我们假设你被列名为ID筛选:

Another option (which also will work on old .NET versions) is filtering your table with built-in RowFilter functionality which supports IN filters. Let's assume you are filtering by column named ID:

// Check if guids.Length > 0
StringBuilder filter = new StringBuilder("ID IN ("); 
foreach (Guid id in guids)
    filter.AppendFormat("Convert('{0}','System.Guid'),", id);
filter.Append(")");

DataTable template = ReadTemplateList();
DataView view = template.DefaultView;
view.RowFilter = filter.ToString();
DataTable table = view.ToTable();

这篇关于从一个数组C#与过滤值的数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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