C#中的DataTable查詢(xún)實(shí)戰(zhàn)教程
DataTable查詢(xún)
工作中遇到了需要進(jìn)行DataTable進(jìn)行查詢(xún)的需求,簡(jiǎn)單研究了一下,最終使用一下方案實(shí)現(xiàn),簡(jiǎn)單記錄一下便于以后使用。
DataTable dt = dataBox.GetDataForDataTable();//獲取DataTable所有數(shù)據(jù),準(zhǔn)備進(jìn)行查詢(xún)
DataRow[] dtRow = dt.Select("調(diào)劑日期=‘"+MediumCode.Text.Trim()+"'");//根據(jù)查詢(xún)條件,篩選出所有滿足條件的列
DataTable dtNew = dt.Clone();//克隆與原表結(jié)構(gòu)相同的新表(不包括數(shù)據(jù))
foreach (DataRow item in dtRow)//把滿足條件的所有列賽到新表中
{
dtNew.ImportRow(item);
}
dataBox.DataBinding(dtNew);//給控件綁定新值(即查詢(xún)結(jié)果)
補(bǔ)充:C# 通過(guò)LINQ對(duì)DataTable數(shù)據(jù)查詢(xún),結(jié)果生成DataTable
我就廢話不多說(shuō)啦,大家還是直接看代碼吧~
var query = from g in dt_stu.AsEnumerable()
group g by new {
t1 = g.Field<string>("STU_ID"),
t2 = g.Field<string>("CLASS_ID")
} into m
select new
{
STU_ID = m.Key.t1,
CLASS_ID=m.Key.t2,
成績(jī)總合計(jì) = m.Sum(a => a.Field<decimal>("成績(jī)")),
優(yōu)秀人數(shù) = m.Count(a => a.Field<decimal>("成績(jī)")>95)
};
DataTable dt_article = UserClass.ToDataTable(query);
/// <summary>
/// LINQ返回DataTable類(lèi)型
/// </summary>
/// <typeparam name="T"> </typeparam>
/// <param name="varlist"> </param>
/// <returns> </returns>
public static DataTable ToDataTable<T>(IEnumerable<T> varlist)
{
DataTable dtReturn = new DataTable();
// column names
PropertyInfo[] oProps = null;
if (varlist == null)
return dtReturn;
foreach (T rec in varlist)
{
if (oProps == null)
{
oProps = ((Type)rec.GetType()).GetProperties();
foreach (PropertyInfo pi in oProps)
{
Type colType = pi.PropertyType;
if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition()
== typeof(Nullable<>)))
{
colType = colType.GetGenericArguments()[0];
}
dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
}
}
DataRow dr = dtReturn.NewRow();
foreach (PropertyInfo pi in oProps)
{
dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue
(rec, null);
}
dtReturn.Rows.Add(dr);
}
return dtReturn;
}
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
C#使用BitConverter與BitArray類(lèi)進(jìn)行預(yù)定義基礎(chǔ)類(lèi)型轉(zhuǎn)換
這篇文章介紹了C#使用BitConverter與BitArray類(lèi)進(jìn)行預(yù)定義基礎(chǔ)類(lèi)型轉(zhuǎn)換的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05
C#實(shí)現(xiàn)BBcode轉(zhuǎn)為Markdown的方法
這篇文章主要給大家介紹了關(guān)于C#實(shí)現(xiàn)BBcode轉(zhuǎn)Markdown的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-02-02
C#面向?qū)ο笤O(shè)計(jì)原則之單一職責(zé)原則
這篇文章介紹了C#面向?qū)ο笤O(shè)計(jì)原則之單一職責(zé)原則,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03
C#使用CefSharp控件實(shí)現(xiàn)爬蟲(chóng)
這篇文章介紹了C#使用CefSharp控件實(shí)現(xiàn)爬蟲(chóng)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06
C# 使用WPF 用MediaElement控件實(shí)現(xiàn)視頻循環(huán)播放
在WPF里用MediaElement控件,實(shí)現(xiàn)一個(gè)循環(huán)播放單一視頻的程序,同時(shí)可以控制視頻的播放、暫停、停止。這篇文章給大家介紹了C# 使用WPF 用MediaElement控件實(shí)現(xiàn)視頻循環(huán)播放,需要的朋友參考下吧2018-04-04

