首页 > 其他 > 详细

Linq-分组统计

时间:2014-04-29 01:20:17      阅读:346      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    public class Plan
    {
        public string ProjectInfoId
        {
            get;
            set;
        }

        public string CommandInfoId
        {
            get;
            set;
        }

        public int value
        {
            get;
            set;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            List<Plan> listPlan = new List<Plan>() {
            new Plan{ProjectInfoId="01", CommandInfoId="01001", value=0},
            new Plan{ProjectInfoId="01", CommandInfoId="01001", value=0},
            new Plan{ProjectInfoId="01", CommandInfoId="01002", value=0},
            new Plan{ProjectInfoId="02", CommandInfoId="02001", value=0},
            new Plan{ProjectInfoId="02", CommandInfoId="02002", value=1},
            new Plan{ProjectInfoId="02", CommandInfoId="02002", value=1},
            };

            var PlanGroups =
                from p in listPlan
                group p by
                new
                {
                    p.ProjectInfoId,
                    p.CommandInfoId

                }
                    into g
                    let condition = g.Sum<Plan>(t => t.value)
                    where condition > 0
                    select new
                    {
                        g.Key,
                        NumProducts = condition            
                    };
          

            foreach (var group in PlanGroups)
            {
                Console.WriteLine(string.Format("{0},{1}:{2}", group.Key.ProjectInfoId.ToString(), group.Key.CommandInfoId.ToString(), group.NumProducts));
            }
        }
    }
}
bubuko.com,布布扣

 

Linq-分组统计,布布扣,bubuko.com

Linq-分组统计

原文:http://www.cnblogs.com/siso/p/3692493.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!