linq join

    数据准备:
    List<dynamic> ls1=new List<dynamic>{
        new {Id = 1,Name = "张三"},
        new {Id = 2,Name = "李四"},
        new {Id = 3,Name = "王五"},
        new {Id = 4,Name = "赵六"}
    };
    List<dynamic> ls2=new List<dynamic>{
        new {Name = "张三", Subject="语文", Score=60},
        new {Name = "张三", Subject="数学", Score=70},
        new {Name = "张三", Subject="外语", Score=80},
        new {Name = "李四", Subject="语文", Score=85},
        new {Name = "王五", Subject="语文", Score=69}
    };

Join

Inner Join

    var ls = from x in ls1
             join y in ls2 on x.Name equals y.Name
             select new {
                 Id = x.Id,
                 Name = x.Name,
                 Subject = y.Subject,
                 Score = y.Score
             };
    // or 
    var ls = ls1.Join(ls2, x=>x.Name, y=>y.Name, (x,y)=>new {
                 Id = x.Id,
                 Name = x.Name,
                 Subject = y.Subject,
                 Score = y.Score
             });

Left Outer Join

    var ls = from x in ls1
             join y in ls2 on x.Name equals y.Name into g
             from z in g.DefaultIfEmpty()
             select new {
                 Id = x.Id,
                 Name = x.Name,
                 Subject = z?.Subject,
                 Score = z?.Score
             };

GroupJoin

    var ls = from x in ls2
             group x by x.Subject into g
             select new {
                 Subject = g.Key,
                 MaxScore = g.Max(x=>x.Score)
             };

Order By

    var ls = from x in ls2
             orderby x.Subject,x.Score descending
             select x

转载于:https://www.cnblogs.com/AnycleLiu/p/9527969.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值