2012-03-19 63 views
-1

循環我有一個數據表:C#中通過不同的值DataTable中

+----------+--------+ 
| Rep Name | Volume | 
+----------+--------+ 
| Alex  |  9 | 
| Alex  |  1 | 
| Alex  |  3 | 
| Liza  |  24 | 
| Liza  |  17 | 
| Pam  |  0 | 
| Pam  |  6 | 
+----------+--------+ 

我在發送一個電子郵件爲每個獨特的人REP NAME

例如:

Hi Alex, Today your volume was 9, and 1, and 3. 

下一封郵件:

Hi Liza, Today your valume was 24, and 17. 

我想通過對REPNAME

類似唯一值的數據表就知道循環的最簡單的方法:

var DistinctList = datatable123.Select(x=> x.repname).Distinct(); 

,然後我會遍歷DistinctList?

+0

@Wesley var DistinctList = datatable123.Select(x => x.repname).Distinct(); – 2012-03-20 00:19:11

+0

@韋斯利也這個我已經嘗試var list = from x in datatable123 group x by x.repname into g select g; – 2012-03-20 00:51:42

回答

4

您應該使用GroupBy完成此任務。

更新:我已經重寫了代碼,以便您現在可以將它用於您的DataTable

var list = from x in datatable123.AsEnumerable() 
      group x by (string) x["repname"] into g 
      select g; 

foreach (var x in list) 
{ 
    Console.WriteLine("Hi {0}, Today your volume was {1}.", x.Key, 
     String.Join(", and ", x.Select(p => p["volume"]))); 
} 
+0

錯誤找不到源類型爲「System.Data.DataTable」的查詢模式的實現。找不到'GroupBy'。 – 2012-03-20 00:45:50

+0

非常感謝您的幫助,但我想我們不能在數據表上做groupbby? – 2012-03-20 00:46:07

+0

@I__你需要將人員查詢到一個對象列表,然後你可以調用groupby。 – 2012-03-20 01:02:37