C# 如何过滤数据表?

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

How I can filter a Datatable?

c#asp.netfilterdatatabledataset

提问by Tarasov

I use a DataTable with Information about Users and I want search a user or a list of users in this DataTable. I try it butit don't work :(

我使用带有用户信息的数据表,我想在此数据表中搜索用户或用户列表。我试过了,但它不起作用:(

Here is my c# code:

这是我的 C# 代码:

 public DataTable GetEntriesBySearch(string username,string location,DataTable table)
        {
            list = null;
            list = table;

            string expression;
            string sortOrder;

            expression = "Nachname = 'test'";
            sortOrder = "nachname DESC";

            DataRow[] rows =  list.Select(expression, sortOrder);

            list = null; // for testing
            list = new DataTable(); // for testing

            foreach (DataRow row in rows)
            {
                list.ImportRow(row);
            }

            return list; 
        }

采纳答案by Kadir

You can use DataView.

您可以使用数据视图。

DataView dv = new DataView(yourDatatable);
dv.RowFilter = "query"; // query example = "id = 10"


http://www.csharp-examples.net/dataview-rowfilter/


http://www.csharp-examples.net/dataview-rowfilter/

回答by Maxim Kornilov

It is better to use DataView for this task.

最好将 DataView 用于此任务。

Example of the using it you can find in this post: How to filter data in dataview

您可以在这篇文章中找到使用它的示例:How to filter data in dataview

回答by Tim Schmelter

If you're using at least .NET 3.5, i would suggest to use Linq-To-DataTableinstead since it's much more readable and powerful:

如果您至少使用 .NET 3.5,我建议Linq-To-DataTable改用它,因为它更具可读性和功能:

DataTable tblFiltered = table.AsEnumerable()
          .Where(row => row.Field<String>("Nachname") == username
                   &&   row.Field<String>("Ort") == location)
          .OrderByDescending(row => row.Field<String>("Nachname"))
          .CopyToDataTable();

Above code is just an example, actually you have many more methods available.

上面的代码只是一个例子,实际上你有更多的方法可用

Remember to add using System.Linq;and for the AsEnumerableextension method a reference to the System.Data.DataSetExtensionsdll (How).

请记住using System.Linq;AsEnumerable扩展方法添加对System.Data.DataSetExtensionsdll的引用(How)。

回答by VolkanCetinkaya

clear:

清除:

list = null; // for testing
list = new DataTable(); // for testing

foreach (DataRow row in rows)
{
    list.ImportRow(row);
}

use:

用:

.CopyToDataTable()

example:

例子:

string _sqlWhere = "Nachname = 'test'";
string _sqlOrder = "Nachname DESC";

DataTable _newDataTable = yurDateTable.Select(_sqlWhere, _sqlOrder).CopyToDataTable();

回答by nghiavt

For anybody who work in VB.NET (just in case)

对于在 VB.NET 中工作的任何人(以防万一)

Dim dv As DataView = yourDatatable.DefaultView

dv.RowFilter ="query" ' ex: "parentid = 0"

回答by Santosh Kumar

Hi we can use ToLower Method sometimes it is not filter.

嗨,我们可以使用 ToLower 方法,有时它不是过滤器。

EmployeeId = Session["EmployeeID"].ToString();
var rows = dtCrewList.AsEnumerable().Where
   (row => row.Field<string>("EmployeeId").ToLower()== EmployeeId.ToLower());

   if (rows.Any())
   {
        tblFiltered = rows.CopyToDataTable<DataRow>();
   }

回答by Dilaksha A

Sometimes you actually want to return a DataTablethan a DataView. So a DataViewwas not good in my case and I guess few others would want that too. Here is what I used to do

有时您实际上想要返回 a DataTablethan a DataView。所以 aDataView对我来说不好,我想很少有人也会想要它。这是我以前做的

myDataTable.select("myquery").CopyToDataTable()

This will filter myDataTablewhich is a DataTable and return a new DataTable

这将过滤myDataTable哪个是 DataTable 并返回一个新的DataTable

Hope someone will find that is useful

希望有人会发现这是有用的