C# operation DataTable method to implement filtering, fetching the first N pieces of data and obtaining the list of data in a specified column
This article describes the method of C# operating the DataTable method to filter, retrieve the first N pieces of data and obtain the specified column data list. Share it for your reference. The specific analysis is as follows:
#region DataTable filtering, sorting returns a new DataTable that meets the conditions, or directly use DefaultView to return according to the conditions./// <summary> /// DataTable filtering, sorting returns a new DataTable that matches the criteria rows///or directly use DefaultView to return according to the conditions/// eg:SortExprDataTable(dt,"Sex='Male'","Time Desc",1)/// </summary> /// <param name="dt">Incoming DataTable</param>/// <param name="strExpr">Filters</param>/// <param name="strSort">Sorting conditions</param>/// <param name="mode">1, directly use DefaultView to return according to conditions, which is more efficient;/// 2,DataTable filter, sorting returns a new DataTable that meets the criteria rows</param>public static DataTable SortDataTable(DataTable dt, string strExpr,string strSort,int mode) { switch (mode) { case 1: //Method 1: Use DefaultView directly to return according to the conditions = strExpr; = strSort; return dt; case 2: //Method 2 DataTable filtering, //Sorting returns a new DataTable that matches the criteria rows. DataTable dt1 = new DataTable(); DataRow[] GetRows = (strExpr, strSort); //Copy the DataTable dt structure does not contain data dt1 = (); foreach (DataRow row in GetRows) { (); } return dt1; default: return dt; } } #endregion
I hope this article will be helpful to everyone's C# programming.
Related Articles
WPF achieves clock effects
This article mainly introduces in detail to everyone the effect of WPF, which has certain reference value. Interested friends can refer to it.2018-02-02Three ways to obtain and generate random numbers in C#
This article mainly introduces three methods of obtaining and generating random numbers in C#. This article explains the Random class generation method, Guid class generation method, and RNGCryptoServiceProvider class generation method. Friends who need it can refer to it.2015-07-07C# to achieve the disk space size
This article mainly introduces the method of C# to obtain disk space size. It is a very practical technique to implement this function based on the method and ManagementClass("Win32_LogicalDisk"). It is a very practical technique. Friends who need it can refer to it.2014-12-12Do you write exception handling code like this?
This article is correct. Did you write exception handling code like this and provide a detailed analysis and introduction. If you need it, please refer to it.2013-05-05Summary of examples of basic C# drawing methods
To implement simple drawing functions in C#, you can use the Graphics class. The following article mainly introduces relevant information about the basic methods of drawing in C#. The article introduces the example code in detail. Friends who need it can refer to it.2022-12-12Example of implementing control drag resize in C# wpf Canvas
This article mainly introduces an example of implementing control drag and resizing in C# wpf Canvas. The example code is introduced in this article in detail, which has certain reference learning value for everyone's learning or work. Friends who need it, please learn with the editor below.2022-08-08Traversing DataSet dataset object instance in C#
This article mainly introduces the traversal of DataSet dataset object instances in C#. I often forget how to operate DataSet. I will write it down and share it here so that friends who need it can refer to it.2014-08-08C# How to use log4net to record logs
This article mainly introduces the methods and steps of using log4net to record logs in C#. The example code is introduced in this article in detail and has certain reference value. Interested friends can refer to it.2021-09-09The main role of this keyword in C#
This keyword is mainly used in C# to refer to the current object, distinguish between fields and local variables, call other constructors, and pass the current object to other methods or constructors. This article focuses on the role of the C# this keyword. Interested friends, let’s take a look.2024-02-02Use C# to draw the earth's rotation effect
This article mainly introduces in detail how to use C# language to draw the effect of the earth's rotation. The sample code in the article is explained in detail and has certain reference value. If you need it, you can learn about it.2023-02-02