我有一個數據表dtFoo,並希望獲得滿足特定條件的行數。獲取數據表中符合特定條件的行數
編輯:此數據不存儲在數據庫中,因此使用SQL不是一個選項。
在過去,我用以下兩種方法來實現:
方法1
int numberOfRecords = 0;
DataRow[] rows;
rows = dtFoo.Select("IsActive = 'Y'");
numberOfRecords = rows.Length;
Console.WriteLine("Count: " + numberOfRecords.ToString());
方法2
int numberOfRecords = 0;
foreach (DataRow row in dtFoo.Rows)
{
if (row["IsActive"].ToString() == "Y")
{
numberOfRecords++;
}
}
Console.WriteLine("Count: " + numberOfRecords.ToString());
我的店是試圖在一些事情上標準化,這是一個已經出現的問題。我想知道哪些方法在性能(以及爲什麼!)方面最好,以及最常用的方法。
另外,是否有更好的方法來達到預期的效果?
優秀的解決方案,你可以讓我知道一些來源,我可以看到許多LINQ的例子嗎? – 2014-03-29 14:21:27
@Saluce,dtFoo.AsEnumerable()。where(expr)does not need to convert enumerable as we can use dtFoo.Where(expr)is already enumerable – 2015-07-14 11:53:54
@Sunny_Sid這是不正確的。你必須明確地轉換爲'Enumerable'來使用'Enumerable.Where'。 – saluce 2015-10-08 13:58:22