欢迎来到银龙的博客

人生三从境界:昨夜西风凋碧树,独上高楼,望尽天涯路。 衣带渐宽终不悔,为伊消得人憔悴。 众里寻他千百度,蓦然回首,那人却在灯火阑珊处。

linq内部联接,分组联接,左外部联接

 Framework版本的EF

总结:左外部连接,内连接>右表有多就会查出来多条

1.左表1条数据,右表0条

 

 左外部联接

 

 

 

 分组联接

 

 

 内部联接

 

 

 

 

2.数据库里1条对多应,因为后来删除了ArticleComment数据重新添加了下面例子里id可能不一样,效果不影响

 

 

 左外部联接

 

 

 

 

 分组联接

 

 内部联接

 

 

 

 

 

------------------------------------------------

下面是简单的代码供大家复制测试上面的代码

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

 

 

posted on 2020-07-27 19:36  银龙科技  阅读(246)  评论(0编辑  收藏  举报

导航