C# 将过滤后的数据从数据集获取到数据表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10317416/
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-08-09 13:21:04  来源:igfitidea点击:

Get filtered data from dataset to datatable

c#ado.netdatatable.net-2.0

提问by palak

How can I filter data from dataset to datatable? like the code->

如何将数据从数据集过滤到数据表?喜欢代码->

DataRow[] dr = DS.Tables[0]
    .Select("STAGENAME='Develop' AND DEVLAPSEDAYS IS NOT NULL");        

How can I use datatable here?

我如何在这里使用数据表?

following code doesn`t reflect changes->

以下代码不反映更改->

DataTable FilteredDataD = DS.Tables[0];
if (FilteredDataD.Rows.Count > 0) {
    FilteredDataD.DefaultView.RowFilter = "STAGENAME='Develop' AND DEVLAPSEDAYS IS NOT NULL";
    FilteredDataD.DefaultView.ToTable();
}

Is is possible to remove a column using above filter,like "STAGENAME='Develop' AND DEVLAPSEDAYS IS NOT NULL" + FilteredDataD.column("col_name")... Suppose I have 5 columns display only 4,I can`t remove col_name from my query.Is there a way?

是否可以使用上述过滤器删除列,例如“STAGENAME='Develop' AND DEVLAPSEDAYS IS NOT NULL”+ FilteredDataD.column("col_name")...假设我有 5 列只显示 4,我无法删除col_name 来自我的查询。有办法吗?

Reply

回复

采纳答案by James Johnson

Try using LINQ instead:

尝试使用 LINQ:

var table = DS.Tables[0].AsEnumerable().Where(
    r => r.Field<string>("STAGENAME") == "Develop" && r.Field<int?>("DEVLAPSEDAYS").HasValue).AsDataView().ToTable();

EDITChanged AsDataViewto AsDataView()for syntactical accuracy.
EDITProvided .NET 2.0 compatible solution

编辑AsDataViewAsDataView()了语法准确性。
编辑提供 .NET 2.0 兼容解决方案

DataTable table = DS.Tables[0];
if (table.Rows.Count > 0)
{
    table.DefaultView.RowFilter = "STAGENAME = 'DEVELOP' AND DEVLAPSEDAYS IS NOT NULL";
    table = table.DefaultView.ToTable(); 
}

回答by Matt

You could write an extension method (using C# 3) like follows:

您可以编写一个扩展方法(使用 C# 3),如下所示:

public static DataTable Filter(this DataTable dataTable, string selectFilter)
{
    var filteredTable = dataTable.Clone();
    var rows = dataTable.Select(selectFilter).ToList();
    rows.ForEach(filteredTable.ImportRow);
    return filteredTable;
}

Then use it like follows:

然后像下面这样使用它:

DataTable dataTable = DS.Tables[0]
    .Filter("STAGENAME='Develop' AND DEVLAPSEDAYS IS NOT NULL");


Update, since you said you are using C# 2.0 (and thus extension methods and LINQ aren't an option) you could use this instead:

更新,因为您说您使用的是 C# 2.0(因此扩展方法和 LINQ 不是一个选项),您可以改用它:

public static DataTable GetFilteredTable(
    DataTable sourceTable, string selectFilter)
{
    var filteredTable = sourceTable.Clone();
    var rows = sourceTable.Select(selectFilter);
    foreach (DataRow row in rows)
    {
        filteredTable.ImportRow(row);
    }
    return filteredTable;
}

DataTable dataTable = GetFilteredTable(
    DS.Tables[0], "STAGENAME='Develop' AND DEVLAPSEDAYS IS NOT NULL");