2014-01-27 58 views
1

我有以下數據在數據表 -LINQ到一個字符串的計數獲得不同的列值

Col1  Col2 
----------------------- 
Pramod | In 
Pramod | In 
Pramod | Out 
Pramod | In 
Pramod | In 
Pramod | Out 
Pankaj | In 
Pankaj | In 
Pankaj | Out 
Abhi | In 
Abhi | In 
Abhi | Out 

我想下面使用LINQ輸出,

Col1 | In | Out 
------------------ 
Pramod | 4 | 2 
Pankaj | 2 | 1 
Abhi | 2 | 1 

請幫

回答

4

組行通過值Col1,然後計算每組中的InOut值:

from r in table.AsEnumerable() 
group r by r.Field<string>("Col1") into g 
select new { 
    Col1 = g.Key, 
    In = g.Count(r => r.Field<string>("Col2") == "In"), 
    Out = g.Count(r => r.Field<string>("Col2") == "Out") 
} 
+0

@SergeyBerezovskiy ..非常感謝你,我發現LINQ對於複雜查詢非常有用,因爲我是LINQ新手,你能推薦一些書來學習LINQ – user3240893

+0

@ user3240893我建議你查看[pluralsight](http ://www.pluralsight.com/training)LINQ教程。他們非常豐富 –