2010-02-24 95 views
14

我得到這個:C#的DataRow空檢查

DataTable dtEntity = CreateDataTable(); 
drEntity = dtEntity.NewRow(); 

然後我將數據添加到行(或沒有)。 很多代碼,真的不知道行內是否有任何東西。 取決於輸入(我從某些文件導入)。 我想這樣做:

if (drEntity`s EVERY CELL IS NOT EMPTY) 
{ 
    dtEntity.Rows.Add(drEntity); 
} 
else 
{ 
    //don't add, will create a new one (drEntity = dtEntity.NewRow();) 
} 

有一些很好的辦法來檢查,如果DataRow中的每一個細胞是空的? 或者我應該foreach,並逐一檢查他們?

回答

21

線沿線的一個簡單的方法:

bool AreAllColumnsEmpty(DataRow dr) 
{ 
if (dr == null) 
{ 
    return true; 
} 
else 
{ 
    foreach(var value in dr.ItemArray) 
    { 
    if (value != null) 
    { 
     return false; 
    } 
    } 
    return true; 
} 
} 

應該給你什麼你之後,並使其「好」(因爲沒有什麼是據我所知,在框架),你可以把它包裝起來作爲一個擴展方法,然後將由此得到的代碼是:

if (datarow.AreAllColumnsEmpty()) 
{ 
} 
else 
{ 
} 
+0

實際上在foreach中的條件應該不止於此。剛剛測試:) 是這樣的:(!value.ToString()= 「」) 如果(!值= NULL){ 如果 { 回報 假; }} – Ash 2010-02-25 09:14:35

+0

@Swoosh,我想這取決於你的「空」的定義。我去了「null」。 =) – Rob 2010-02-25 09:19:58

+1

這不考慮列的默認值,也不會自動增加列 - 請參閱我的答案。 – Joe 2012-07-13 13:45:28

0

AFAIK,在框架中沒有這樣做的方法。即使在框架中支持這樣的事情,它基本上也會做同樣的事情。這將查看DataRow中的每個單元格以查看它是否爲空。

1

您可以使用此:

if(drEntity.ItemArray.Where(c => IsNotEmpty(c)).ToArray().Length == 0) 
{ 
    // Row is empty 
} 

IsNotEmpty(cell)將是您自己的實現,根據單元格中的數據類型檢查數據是否爲空或空。如果它是一個簡單的字符串,它最終可能看起來像這樣:

if(drEntity.ItemArray.Where(c => c != null && !c.Equals("")).ToArray().Length == 0) 
{ 
    // Row is empty 
} 
else 
{ 
    // Row is not empty 
} 

儘管如此,它基本上是檢查空虛每個細胞,並讓您知道該行中的所有單元格是否爲空。

+0

有一件事我不喜歡這樣的做法是,你確實有* *想以確定代碼做什麼,而「如果(IsDataRowEmpty(drEntity)){}其他{}」與* *實施搬進IsDataRowEmpty方法更容易閱讀。 – Rob 2010-02-25 09:22:30

0

也許更好的解決方案是添加一個額外的列,在每一行上自動設置爲1。只要存在着是不是空更改爲0。

然後

If(drEntitity.rows[i].coulmn[8] = 1) 
{ 
    dtEntity.Rows.Add(drEntity); 
} 
else 
{ 
    //don't add, will create a new one (drEntity = dtEntity.NewRow();) 
} 
5
public static bool AreAllCellsEmpty(DataRow row) 
{ 
    if (row == null) throw new ArgumentNullException("row"); 

    for (int i = row.Table.Columns.Count - 1; i >= 0; i--) 
    if (!row.IsNull(i)) 
     return false; 

    return true; 
} 
0

我做了這樣的一個元素:

var listOfRows = new List<DataRow>(); 
foreach (var row in resultTable.Rows.Cast<DataRow>()) 
{ 
    var isEmpty = row.ItemArray.All(x => x == null || (x!= null && string.IsNullOrWhiteSpace(x.ToString()))); 
    if (!isEmpty) 
    { 
     listOfRows.Add(row); 
    } 
} 
+0

它應該檢查它是否爲!isEmpty,然後將該行添加到列表中。 – Jeff 2011-07-27 20:56:23

6

我喜歡湯米的方法卡里爾,但有一點變化。

foreach (DataColumn column in row.Table.Columns) 
    if (!row.IsNull(column)) 
     return false; 

    return true; 

我想這種方法看起來更簡單明瞭。

1

DataTable.NewRow將初始化每個字段:

  • 每個DataColumnDataColumn.DefaultValue

  • 除了自動遞增列(DataColumn.AutoIncrement == true)的默認值,這將被初始化到下一自動增值。

  • 和表達列(DataColumn.Expression.Length > 0)也是一種特殊情況;默認值將取決於計算表達式的列的默認值。

所以,你可能應該檢查類似:

bool isDirty = false; 
for (int i=0; i<table.Columns.Count; i++) 
{ 
    if (table.Columns[i].Expression.Length > 0) continue; 
    if (table.Columns[i].AutoIncrement) continue; 
    if (row[i] != table.Columns[i].DefaultValue) isDirty = true; 
} 

我將離開LINQ版本作爲一個練習:)

3

我知道這已經被回答,它是一個老的問題,但這裏的擴展方法做同樣的:

public static class DataExtensions 
{ 
    public static bool AreAllCellsEmpty(this DataRow row) 
    { 
     var itemArray = row.ItemArray; 
     if(itemArray==null) 
      return true; 
     return itemArray.All(x => string.IsNullOrWhiteSpace(x.ToString()));    
    } 
} 

你使用它像這樣:

if (dr.AreAllCellsEmpty()) 
// etc 
7

我創建了一個幫手(靜態類我叫DataRowHelpers,創造性,我知道)呼籲IsEmpty如下:

public static bool IsEmpty(this DataRow row) 
{ 
    return row == null || row.ItemArray.All(i => i is DBNull); 
} 

這裏其他的答案是正確的。我只是簡單地將Linq用於對象,我覺得它簡潔。順便說一句,這與Excel解析結合使用非常有用,因爲用戶可能會在頁面上放置一行(數千行)而不考慮解析數據的方式。

在同一個班,我把其他的幫手,我發現有用的,像這樣的解析器,如果字段包含你知道應該是一些文本,你能流利地解析它。對於任何新創意的人,小專業提示。 (?!任何人在SO,真是羅)

考慮到這一點,這裏是一個增強版本:

public static bool IsEmpty(this DataRow row) 
{ 
    return row == null || row.ItemArray.All(i => i.IsNullEquivalent()); 
} 

public static bool IsNullEquivalent(this object value) 
{ 
    return value == null 
      || value is DBNull 
      || string.IsNullOrWhiteSpace(value.ToString()); 
} 

現在你有另一個有用的幫手,IsNullEquivalent可以在這種情況下和任何其他可用於也是。如果你知道你的數據有這樣的佔位符,你可以擴展它來包含諸如"n/a""TBD"之類的東西。