2013-11-23 60 views

回答

4

試試這個:

var result = data.Where(a => a.id == 3).Sum(a => a.rs); 
+0

但我執行此查詢datatable然後? –

5

這應該工作:

var sum = data.Where(x=>x.id==3).Sum(x=>x.rs) 

更新:

你需要的東西叫做LINQ到數據表 這裏舉例:

var dt = new Datatable(); 
//fill dt here 
//row -> every row in datatable 
//row.Field<T>(columnName) -> access to the specific row cell from the column of name 'columnName' and of type T 

var sum = dt.AsEnumerable().Where(row=>row.Field<string>("Id")=="3").Sum(row=>row.Field<int>("rs")); 

更多這裏:http://msdn.microsoft.com/pl-pl/library/bb399401(v=vs.110).aspx

+0

但我執行此查詢datatable然後? –

+0

假設我的數據表名是dt,並且我想在數據表上執行相同的查詢,那我該怎麼做呢? –

+0

@HirenRaiyani你有沒有創建DbContext或模型? – Uriil