首页 > 其他 > 详细

linq左连接

时间:2020-07-27 23:58:43      阅读:74      评论:0      收藏:0      [点我收藏+]

 

代码

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

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Pet> pets = new List<Pet>{ new Pet { Name="A", Age=1 },
                                            new Pet { Name="B", Age= 2},
                                            new Pet { Name="C", Age=3 } };

            List<Pet2> pets2 = new List<Pet2>{  new Pet2 { Name="A", Sex = "" },
                                                new Pet2 { Name="A", Sex = ""},
                                                 new Pet2 { Name="B", Sex = "" }, };

            var query = from p1 in pets
                        join pet2 in pets2 on p1.Name equals pet2.Name
                        select new { p1, pet2 };
            var list = query.ToList();
            Console.WriteLine(list.Count);
            foreach (var item in list)
            {
                Console.WriteLine($"pet={item.p1.Name},{item.p1.Age} pet2={item.pet2.Name},{item.pet2.Sex}");
            }
            Console.WriteLine("Hello World!");
        }
    }

    class Pet
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
    class Pet2
    {
        public string Name { get; set; }
        public string Sex { get; set; }
    }
}

 

技术分享图片

 

linq左连接

原文:https://www.cnblogs.com/wangyinlon/p/13387236.html

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