siso

明天会更好!

Linq-分组统计

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));
            }
        }
    }
}

 

posted on 2014-04-26 21:22  siso  阅读(593)  评论(0编辑  收藏  举报

导航