C#過(guò)濾DataTable中空數(shù)據(jù)和重復(fù)數(shù)據(jù)的示例代碼
C#過(guò)濾DataTable中的空數(shù)據(jù)和重復(fù)數(shù)據(jù)
string sql = "select name,age from user"; DataTable data = DB.ExecuteDataTable(string.Format(sql)); //得到DataTable // ------------start 去重------------------- string[] distinctcols = new string[(data.Columns.Count)]; foreach (DataColumn dc in data.Columns) { distinctcols[dc.Ordinal] = dc.ColumnName; } DataView mydataview = new DataView(data); DataTable data1 = mydataview.ToTable(true, distinctcols); // ------------end ------------------- // ------------start 去null------------------- DataTable data2 = data1.Clone();//克隆表 foreach (DataRow drItem in data1.Rows) { //過(guò)濾,判斷條件的下標(biāo)自己控制 if (!string.IsNullOrWhiteSpace(drItem[0].ToString()) && !string.IsNullOrWhiteSpace(drItem[1].ToString())) { data2.Rows.Add(drItem.ItemArray); } } // ------------end------------------- /** 下面沒(méi)啥用,增加一行空白行,搜索的時(shí)候當(dāng)全部搜索用 */ DataRow row = data2.NewRow(); data2.Rows.InsertAt(row, 0); return data2;
附:c# datatable根據(jù)某個(gè)條件過(guò)濾數(shù)據(jù)
判斷DataTable中某一行某列的數(shù)據(jù)為空值的辦法
需要使用DataRow類自帶的一個(gè)函數(shù)IsNull。
if(!DataRow.IsNull(index)) if(Convert.IsDBNull(Row.ItemArray[index]))
在DataReader(如SqlDataReader)有相同功能的IsDBNull函數(shù)
datatable根據(jù)某個(gè)條件過(guò)濾數(shù)據(jù)
public static DataTable TblFilter(DataTable sourceTable, string condition) { var tempDt = sourceTable.Clone(); var rows = sourceTable.Select(condition); foreach (var dr in rows) { tempDt.ImportRow(dr); } return tempDt; } public static DataTable TblFilter(DataTable sourceTable, string condition, string[] columns) { var tempDt = new DataTable(); foreach (var t in columns) { tempDt.Columns.Add(t, typeof(String)); } var rows = sourceTable.Select(condition); foreach (var dr in rows) { var newDr = tempDt.NewRow(); foreach (var t in columns) { newDr[t.Split(':')[0]] = dr[t.Split(':')[0]].ToString(); } tempDt.Rows.Add(newDr); } return tempDt; }
sql語(yǔ)句中l(wèi)eft,right函數(shù)取字段的左或者右?guī)孜粩?shù)字
LEFT(shopid,4)
總結(jié)
到此這篇關(guān)于C#過(guò)濾DataTable中空數(shù)據(jù)和重復(fù)數(shù)據(jù)的文章就介紹到這了,更多相關(guān)C#過(guò)濾DataTable空數(shù)據(jù)和重復(fù)數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
實(shí)例解析C#設(shè)計(jì)模式編程中簡(jiǎn)單工廠模式的使用
這篇文章主要介紹了C#設(shè)計(jì)模式編程中簡(jiǎn)單工廠模式的使用,文中也舉了在.NET框架下簡(jiǎn)單工廠模式的實(shí)現(xiàn)例子,需要的朋友可以參考下2016-02-02Unity UGUI的ScrollRect滾動(dòng)視圖組件使用詳解
這篇文章主要為大家介紹了Unity UGUI的ScrollRect滾動(dòng)視圖組件使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07C#自定義函數(shù)NetxtString生成隨機(jī)字符串
這篇文章主要介紹了C#自定義函數(shù)NetxtString生成隨機(jī)字符串,是十分常見(jiàn)的重要功能,需要的朋友可以參考下2014-08-08C#利用IDbCommand實(shí)現(xiàn)通用數(shù)據(jù)庫(kù)腳本執(zhí)行程序
在.net 應(yīng)用中,在數(shù)據(jù)庫(kù)中執(zhí)行腳本程序是經(jīng)常用到的功能,如數(shù)據(jù)操作(新增、修改、刪除等),執(zhí)行一個(gè)存儲(chǔ)過(guò)程等,本文將介紹如何通過(guò)利用IDbCommand 實(shí)現(xiàn)通用數(shù)據(jù)庫(kù)腳本執(zhí)行程序,感興趣的朋友可以參考下2024-04-04C#實(shí)現(xiàn)Menu和ContextMenu自定義風(fēng)格及contextMenu自定義
ContextMenu 類表示當(dāng)用戶在控件或窗體的特定區(qū)域上單擊鼠標(biāo)右鍵時(shí)會(huì)顯示的快捷菜單,要想實(shí)現(xiàn)自定義的Menu和ContextMenu效果,大家可以通過(guò)派生ProfessionalColorTable類,下面小編把實(shí)現(xiàn)Menu和ContextMenu自定義風(fēng)格及ContextMenu自定義給大家整理一下2015-08-08