如何在c#中编写简单的组sql查询到linq查询

Hir*_*ani -3 c# linq datatable

我们只是通过db表上的查询来执行任何组

select sum(rs) from data where id=3;
Run Code Online (Sandbox Code Playgroud)

如何使用LINQ在datatable上编写类似的查询?

Grz*_*rzM 5

这应该工作:

var sum = data.Where(x=>x.id==3).Sum(x=>x.rs)
Run Code Online (Sandbox Code Playgroud)

更新:

你需要一个名为Linq to Datatable Example的东西:

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"));
Run Code Online (Sandbox Code Playgroud)

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