C#中使用LINQ和lambda实现左链接、右链接、内链接

小白最近又面试了一个外包公司,这公司上来就问,.NET Mvc传值方式是啥?给小白问蒙了,因为小白最近做的比较多的是MVC API,他想了半天才达到“viewBag,viewDate,Modle等”。答完后又问小白LINQ中怎么实现左连接,这个问题小白实践过,由于间隔时间太长还真忘记了,于是晚上回来问笔者。首先感觉这个面试官在读题面试,这种基础的问题在实践中一般程序员都会,如果背答案,要用语言描述出来就有点难了,好比问“你在的县里有几个乡?”没有几个人能答出来。这篇文章举例实现LINQ的左链接、右链接、内链接。.

 实例

需求是有两个表,客户表和客户的联系方式表,一个客户可能有多个联系方式,联系方式表里可能不含现有的客户,关联关系是联系人表中的CustomerId是客户表的Id,也就是外键。然后拿这两个表来实现左链接、右链接、内链接,我们这里在控制台程序中模拟两个类和模拟数据。代码如下:

//客户类public class Customer    {        public int id { get; set; }        public string name { get; set; }        public string email { get; set; }    }    //联系方式类  public class CustomerContact    {        public int ccid { get; set; }        public int CustomerId { get; set; }//客户的id,外键        public string Phone { get; set; }        public string Address { get; set; }    }   //模拟数据           public static List<Customer> GetCustomer()//客户        {            List<Customer> list = new List<Customer>();            list.Add(new Customer { id = 1, name = "刘德华",  email = "ldh@net.cn" });            list.Add(new Customer { id = 2, name = "张学友",  email = "zxy@net.cn" });            list.Add(new Customer { id = 3, name = "黎明",  email = "lm@net.cn" });            list.Add(new Customer { id = 4, name = "郭富城",  email = "gfc@net.cn" });            list.Add(new Customer { id = 4, name = "古天乐",  email = "gtl@net.cn" });            return list;        }        public static List<CustomerContact> GetCustomerContact()//联系人        {            List<CustomerContact> list = new List<CustomerContact>();            list.Add(new CustomerContact { ccid = 1,CustomerId=1, Phone="13566769988",Address="北京"});            list.Add(new CustomerContact { ccid = 2, CustomerId = 1, Phone = "13566769986", Address = "天津" });            list.Add(new CustomerContact { ccid = 3, CustomerId =2, Phone = "13677889900", Address = "香港" });            list.Add(new CustomerContact { ccid = 4, CustomerId = 8, Phone = "13677889332", Address = "上海" });            return list;        }

数据准备好了,我们接下来实现一下。

一、左连接:

直接上代码

 var LeftJoin = from cusetomer in GetCustomer()                join cc in GetCustomerContact()                on cusetomer.id equals cc.CustomerId into JoinCC                from cc in JoinCC.DefaultIfEmpty()                select new                {                    CustomerName = cusetomer.name,                    phone = cc != null ? cc.Phone : null                };

输出结果:

C#中使用LINQ和lambda实现左链接、右链接、内链接

二、右连接:

右链接跟左链接相反,只需要改一下顺序就可以了。

 var LightJoin = from cc in GetCustomerContact()                 join cusetomer in GetCustomer()                 on cc.CustomerId equals cusetomer.id into JoinCC                from cusetomer in JoinCC.DefaultIfEmpty()                select new                {                    phone = cc.Phone,                    CustomerName =cusetomer != null ? cusetomer.name : null,                };

C#中使用LINQ和lambda实现左链接、右链接、内链接

三、内连接:

内链接就简单多了,直接用join,不用使用DefaultIfEmpty函数设置默认了

 var InnerJoin = from cc in GetCustomerContact()                 join cusetomer in GetCustomer()                 on cc.CustomerId equals cusetomer.id                  select new                 {                     phone = cc.Phone,                     CustomerName = cusetomer.name                 };

C#中使用LINQ和lambda实现左链接、右链接、内链接

四、使用lambda表达式实现

1、左连接

感觉简单多了,比linq的写法舒服。右链接我们这里就不再列出,跟左链接写法相反。

 var LMLeftJoin = GetCustomer().GroupJoin(GetCustomerContact(),  a => a.id, b => b.CustomerId, (a, b) =>new { a,b })     .SelectMany(c=>c.b.DefaultIfEmpty(), (c, b)      =>new { CustomerName= c.a.name,Phone=b!=null?b.Phone:null });

输出结果同上的linq的左连接

2、内链接

var LMInnerJoin = GetCustomer().Join(GetCustomerContact(), a => a.id, b => b.CustomerId, (a, b) => new { CustomerName = a.name, Phone = b.Phone });

输出结果同上linq的内链接

五、结语

      面试是千变万化,作为程序员还是需要提高自己的业务水平和技巧,在面试之前针对面试要求做好准备,背答案也是可以有的,万一遇到一个二逼面试官呢?  本文介绍了在C#中使用LINQ和lambda实现左链接、右链接、内链接的实例,如何跟面试官交流呢,大家想想怎么把代码转换成语言说出来,欢迎留言?希望本文对大家学习和工作有一定参考价值,同时欢迎大家留言讨论,谢谢大家的支持。

源码链接:https://pan.baidu.com/s/14F7zNh8cli0zCbN-Wkt3uQ?pwd=9xsu
提取码:9xsu