【笔记】DataTable或IList使用GroupBy方法的lamda表达式

DataTable GroupBy的用法

            var result = dt.AsEnumerable().
                GroupBy(g => new
                {
                    StaffID = g.Field<string>("StaffID"),
                    StaffName = g.Field<string>("StaffName"),
                    ActivityDate = g.Field<string>("ActivityDate")
                }).
                Select(n => new DailyRouteDisplayItem
                {
                    ActivityDate = n.Key.ActivityDate,
                    StaffID = n.Key.StaffID,
                    StaffName = n.Key.StaffName,
                    RouteList = n.Select(x => new RouteDisplayItem
                    {
                        StaffID = x["StaffID"].ToString(),
                        StaffName = x["StaffName"].ToString(),
                        ActivityID = x["ActivityID"].ToString(),
                        ActivityDate = x["ActivityDate"].ToString(),
                        CheckinType = x["CheckinType"].ToString(),
                        CheckinTime = x["CheckinTime"].ToString(),
                        CheckinLat = ConvertObject.ToDecimal(x["CheckinLat"]),
                        CheckinLng = ConvertObject.ToDecimal(x["CheckinLng"]),
                        CheckoutType = x["CheckoutType"].ToString(),
                        CheckoutTime = x["CheckoutTime"].ToString(),
                        CheckoutLat = ConvertObject.ToDecimal(x["CheckoutLat"]),
                        CheckoutLng = ConvertObject.ToDecimal(x["CheckoutLng"])
                    }).ToList()
                }).ToList();

  

数组GroupBy的用法

            var result = list.GroupBy(n => new { n.ActivityDate, n.StaffID, n.StaffName }).Select(n => new DailyRouteDisplayItem
            {
                ActivityDate = n.Key.ActivityDate,
                StaffID = n.Key.StaffID,
                StaffName = n.Key.StaffName,
                RouteList = n.ToList()
            }).ToList();

  



posted on 2018-03-22 15:29  Vengen  阅读(191)  评论(0编辑  收藏  举报

导航