代码参考:

using System;
using System.Collections.Generic;
using System.Linq;

namespace LinqGroupByDemp
{
    class Dorm
    {
        public string Id { get; set; }
        public int NotNullBedNum { get; set; }
        public string  DormName { get; set; }
       
    }
    class StuDorm
    {
        public string Id { get; set; }
        public string StuName { get; set; }
        public string DormId { get; set; }
        public int NotNullBedNum { get; set; }
        public string DormName { get; set; }

    }
    class Program
    {
        static void Main(string[] args)
        {
            List<Dorm> dorm = new List<Dorm>
            {
                new Dorm {Id="11",NotNullBedNum=4,DormName="N11"},
                new Dorm {Id="22",NotNullBedNum=888,DormName="N22"},
                new Dorm {Id="33",NotNullBedNum=2,DormName="N33"},
                new Dorm {Id="44",NotNullBedNum=5,DormName="N44"},
                new Dorm {Id="55",NotNullBedNum=888,DormName="N55"},
                new Dorm {Id="66",NotNullBedNum=888,DormName="N66"},
                new Dorm {Id="77",NotNullBedNum=888,DormName="N77"},
                new Dorm {Id="88",NotNullBedNum=888,DormName="N88"},
                new Dorm {Id="99",NotNullBedNum=888,DormName="N99"}            
            };

            List<StuDorm> stuDorm = new List<StuDorm>
            {
                new StuDorm {Id="1",StuName="张三",DormId="11",NotNullBedNum=4,DormName="N11"},
                new StuDorm {Id="2",StuName="李四",DormId="11",NotNullBedNum=4,DormName="N11"},
                new StuDorm {Id="3",StuName="王志",DormId="11",NotNullBedNum=4,DormName="N11"},
                new StuDorm {Id="4",StuName="王大伟",DormId="33",NotNullBedNum=2,DormName="N33"},
                new StuDorm {Id="5",StuName="张长为",DormId="44",NotNullBedNum=5,DormName="N44"},
                new StuDorm {Id="6",StuName="高小军",DormId="44",NotNullBedNum=5,DormName="N44"},
                new StuDorm {Id="7",StuName="汪小韩",DormId="11",NotNullBedNum=4,DormName="N11"},
                new StuDorm {Id="8",StuName="刘坤",DormId="44",NotNullBedNum=5,DormName="N44"},
                new StuDorm {Id="9",StuName="赵涛",DormId="33",NotNullBedNum=2,DormName="N33"},
                new StuDorm {Id="10",StuName="党利",DormId="44",NotNullBedNum=5,DormName="N44"},
                new StuDorm {Id="11",StuName="吴利",DormId="44",NotNullBedNum=5,DormName="N44"}
            }; 


            var q = from dM in dorm
                    join sD in stuDorm
                    on dM.Id equals sD.DormId into g
                    from o in g.DefaultIfEmpty(new StuDorm { Id = "00", StuName = "xx", DormId = "yy", NotNullBedNum = -1, DormName = "zz" })
                    select new
                    {
                        DormId = dM.Id,
                        StuDormId=o.Id,
                        NotNullBedNum = o.NotNullBedNum,
                        DormName = dM.DormName
                    };

            foreach (var e in q)
            {
                Console.WriteLine("{0}------{1}---------{2}-----------{3}", e.DormId, e.StuDormId, e.NotNullBedNum, e.DormName);
            }

            var rr = from e in q
                     where e.StuDormId != "00"
                     group e by e.DormId into gg                   
                    select new
                    {
                        DormId = gg.Key,
                        NotNullBedNum = gg.Count()
                    };


            foreach (var ee in rr)
            {
                Console.WriteLine("{0}======{1}", ee.DormId, ee.NotNullBedNum);          

            }

            var rrr = from e in q
                     where e.StuDormId=="00"                     
                     select new
                     {
                         DormId = e.DormId,                  
                         NotNullBedNum =0
                     };

            var a = rr.Union(rrr);
            foreach (var ee in a)
            {
                Console.WriteLine("{0}/////////////////////////////{1}", ee.DormId, ee.NotNullBedNum);            

            }


        }
    }
}

 

原文地址:http://www.cnblogs.com/exesoft/p/16800935.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性