lambda Join GroupJoin

Join

 两个互相Join的集合顺序随意,不过后面的key ,以及返回结果函数  参数的顺序要和join的顺序一致

 // 初始化员工信息
            var persons = new[] { new Person { Id = 1, Name = "张三" }, new Person { Id = 2, Name = "李四" }, new Person { Id = 3, Name = "王二" } };

            // 初始化员工的联系方式
            var contactInfos = new[]
            {
            new ContactInformation {PersonId = 1, Address = "上海", PhoneNumber = "001-00001"},
            new ContactInformation {PersonId = 2, Address = "北京", PhoneNumber = "002-00002"},
            new ContactInformation {PersonId = 3, Address = "广州", PhoneNumber = "003-00003"},
            new ContactInformation {PersonId = 1, Address = "深圳", PhoneNumber = "004-00004"}
        };

            //var joinResult = contactInfos.Join(persons, contactInfo => contactInfo.PersonId, person => person.Id, (information, person) => new
            //{
            //    Name = person.Name,
            //    PhoneNumber = information.PhoneNumber,
            //    Address = information.Address
            //});
            var joinResult =  persons.Join(contactInfos, person => person.Id, contactInfo => contactInfo.PersonId, (person,information ) => new
            {
                Name = person.Name,
                PhoneNumber = information.PhoneNumber,
                Address = information.Address
            });

            foreach (var item in joinResult)
            {
                Console.WriteLine($"姓名:{item.Name},电话:{item.PhoneNumber},地址:{item.Address}");
            }



姓名:张三,电话:001-00001,地址:上海
姓名:张三,电话:004-00004,地址:深圳
姓名:李四,电话:002-00002,地址:北京
姓名:王二,电话:003-00003,地址:广州

GroupJoin   用第一个集合的键 去 匹配所有第二个集合的键(可能多个)  

参数说明:

复制代码

outer
Type: System.Collections.Generic.IEnumerable<TOuter>
要联接的第一个序列。
inner
Type: System.Collections.Generic.IEnumerable<TInner>
要与第一个序列联接的序列。
outerKeySelector
Type: System.Func<TOuter, TKey>
用于从第一个序列的每个元素提取联接键的函数。
innerKeySelector
Type: System.Func<TInner, TKey>
用于从第二个序列的每个元素提取联接键的函数。
resultSelector
Type: System.Func<TOuter, IEnumerable<TInner>, TResult>
用于从第一个序列的元素和第二个序列的匹配元素集合中创建结果元素的函数。
返回值
Type: System.Collections.Generic.IEnumerable<TResult>
IEnumerable<T> ,其中包含类型的元素 TResult 通过对两个序列执行分组的联接获得的。
// 初始化员工信息
            var persons = new[] { new Person { Id = 1, Name = "张三" }, new Person { Id = 2, Name = "李四" }, new Person { Id = 3, Name = "王二" } };

            // 初始化员工的联系方式
            var contactInfos = new[]
            {
            new ContactInformation {PersonId = 1, Address = "上海", PhoneNumber = "001-00001"},
            new ContactInformation {PersonId = 2, Address = "北京", PhoneNumber = "002-00002"},
            new ContactInformation {PersonId = 3, Address = "广州", PhoneNumber = "003-00003"},
            new ContactInformation {PersonId = 1, Address = "深圳", PhoneNumber = "004-00004"}
        };

            var groupJoinResult = persons.GroupJoin(contactInfos, person => person.Id, contactInfo => contactInfo.PersonId, (person, infos) => new
            {
                Name = person.Name,
                Phones = string.Join(",", infos.Select(x => x.PhoneNumber))
            });

            foreach (var item in groupJoinResult)
            {
                Console.WriteLine($"姓名:{item.Name},电话:{item.Phones}");
            }

姓名:张三,电话:001-00001,004-00004
姓名:李四,电话:002-00002
姓名:王二,电话:003-00003

 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值