linq group by 两个值
2010-06-02 10:24
281 查看
DataSetHelper dsHelper = DataSetHelper(); List<M_Order> listSrc = new List<M_Order>(); listSrc = (List<M_Order>)ModelConvertHelper<M_Order>.ConvertToModel(ds.Tables[0]); var sums = listSrc.GroupBy(x => new { x.Name }) .Select(group => new { Keys= group.Key, OrderCount = group.Sum(x => x.OrderCount) }); #region 类 private class M_Order { public int OrderCount { get; set; } public string Name { get; set; } public string Date { get; set; } } #endregion
namespace common { public class ModelConvertHelper<T> where T : new() { public static IList<T> ConvertToModel(DataTable dt) { // 定义集合 IList<T> ts = new List<T>(); // 获得此模型的类型 Type type = typeof(T); string tempName = ""; foreach (DataRow dr in dt.Rows) { T t = new T(); // 获得此模型的公共属性 PropertyInfo[] propertys = t.GetType().GetProperties(); foreach (PropertyInfo pi in propertys) { tempName = pi.Name; // 检查DataTable是否包含此列 if (dt.Columns.Contains(tempName)) { // 判断此属性是否有Setter if (!pi.CanWrite) continue; object value = dr[tempName]; if (value != DBNull.Value) pi.SetValue(t, value, null); } } ts.Add(t); } return ts; } } }
http://stackoverflow.com/questions/363655/c-list-groupby-2-values
相关文章推荐
- LINQ体验(7)——LINQ to SQL语句之Group By/Having和Exists/In/Any/All/Contains
- linq.js groupby使用
- linq group by max 多表链接实例
- LINQ to SQL语句之Group By/Having和Exists/In/Any/All/Contains
- 5、步步为营VS 2008 + .NET 3.5(5) - LINQ查询操作符之Select、Where、OrderBy、OrderByDescending、GroupBy、Join、GroupJoin及其对应的查询语法
- LINQ体验(7)--LINQ to SQL语句之Group By/Having和Exists/In/Any/All/Conta...
- LINQ系列:LINQ to SQL Group by/Having分组
- linq group by
- 通过Linq 实现DataTable Group By
- LINQ group by sum
- [转]LINQ体验(7)——LINQ语句之Group By/Having和Exists/In/Any/All/Contains
- linq group by多个字段,返回多个字段.
- LinQ to Object --- 有关于Dictionary、SQL(LEFT OUTER JOIN \ Group OrderBy Sum) 操作
- Linq Group by 用法
- LINQ语句之Group By/Having和Exists/In/Any/All/Contains
- linq to sql用法解析---group by
- LINQ体验(7)——LINQ to SQL语句之Group By/Having和Exists/In/Any/All/Contains (转载)
- LinQ 标准的查询操作符 分组group by into, select new,orderby descending,from in
- linq to datatable group by 多列 实现
- 【转】LINQ to SQL语句(6)之Group By/Having