欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#過(guò)濾DataTable中空數(shù)據(jù)和重復(fù)數(shù)據(jù)的示例代碼

 更新時(shí)間:2021年01月06日 11:05:21   作者:婉的裕  
這篇文章主要給大家介紹了關(guān)于C#過(guò)濾DataTable中空數(shù)據(jù)和重復(fù)數(shù)據(jù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

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)文章

最新評(píng)論