Lambda表达式多表连接的左连

Posted zunzunq

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Lambda表达式多表连接的左连相关的知识,希望对你有一定的参考价值。

  在网上有很多人都在查找Lambda的例子,但是完整的例子不多,况且还有相当一部分几乎完全不能用,LINQ的左连倒是挺多的,但是LINQ的代码相对比较少,一旦遇到重复数据的时候,不容易被过滤,Lambda就可以轻松避免这个让人头疼的问题。废话不多说,看下面的例子吧。

                    var model_main = from o in db.Main where o.OrderIsdel == 0 select o;
                    var model_user = from u in db.User select u;
                    var model_device = from d in db.Device select d;
                    var model_team = from t in db.Team select t;
					var sub_model = from sub in db.SubOrder select sub;

                    var model_order_user = model_order.GroupJoin(model_user, o => o.OrderUserId, u => u.UserId, (o, u) => new { o, u }).SelectMany(p => p.u.DefaultIfEmpty(), (p, u) => new OrderModel()
                    {
                        OrderDeviceId = p.o.OrderDeviceId,
                        OrderId = p.o.OrderId,
                        OrderUserId = p.o.OrderUserId,
                        OrderTeamID = p.o.OrderTeamID,
                        OrderUserNickName = u.UserName,
                    });

                    var model_order_user_device = model_order_user.GroupJoin(model_device, o => o.OrderDeviceId, d => d.DeviceId, (o, d) => new { o, d }).SelectMany(p => p.d.DefaultIfEmpty(), (p, d) => new OrderModel()
                    {
                        OrderDeviceId = p.o.OrderDeviceId,
                        OrderId = p.o.OrderId,
                        OrderUserId = p.o.OrderUserId,
                        OrderTeamID = p.o.OrderTeamID,
                        OrderUserNickName = p.o.OrderUserNickName,
                        DeviceName = d.DeviceName,
                        DeviceAddress = d.DeviceAddress,
                        DeviceZone = d.DeviceZone
                    });

                    var model_order_user_device_team = model_order_user_device.GroupJoin(model_team, o => o.OrderTeamID, t => t.TeamId, (o, t) => new { o, t }).SelectMany(p => p.t.DefaultIfEmpty(), (p, t) => new OrderModel()
                    {
                        OrderDeviceId = p.o.OrderDeviceId,
                        OrderId = p.o.OrderId,
                        OrderUserId = p.o.OrderUserId,
                        OrderTeamID = p.o.OrderTeamID,
                        OrderUserNickName = p.o.OrderUserNickName,
                        DeviceName = p.o.DeviceName,
                        DeviceAddress = p.o.DeviceAddress,
                        DeviceZone = p.o.DeviceZone,
                        TeamSerisNumber = t.TeamSerisNumber,
                    }); 
                    
                    var model = model_order_user_device_team.GroupJoin(sub_model, o => o.OrderId, s => s.SubOrderParentId, (o, s) => new { o, s }).SelectMany(p => p.s.DefaultIfEmpty(), (p, s) => new OrderModel()
                    {
                        OrderDeviceId = p.o.OrderDeviceId,
                        OrderId = p.o.OrderId,
                        OrderUserId = p.o.OrderUserId,
                        OrderTeamID = p.o.OrderTeamID,
                        OrderUserNickName = p.o.OrderUserNickName,
                        DeviceName = p.o.DeviceName,
                        DeviceAddress = p.o.DeviceAddress,
                        DeviceZone = p.o.DeviceZone,
                        GoodsCount = sub_model.Where(sub => sub.SubOrderParentId == p.o.OrderId).Sum(sub => sub.SubOrderGoodsNumber),
                        
                    });

  

GroupJoin前面部分是左连的集合,里面的参数分别是右边的集合、左边集合的外键、右边集合的主键、两个集合的对象、查找的集合。
写的不好,如何有何问题,大家可以一起讨论,这个只是作为笔记使用而已。

以上是关于Lambda表达式多表连接的左连的主要内容,如果未能解决你的问题,请参考以下文章

Linq中的lambda /方法语法中的左外连接[重复]

利用EF Core的Lambda表达式和Join进行多表查询

利用EF Core的Lambda表达式和Join进行多表查询

使用实体框架 4.1 进行多表连接,我应该使用 lambda 还是 LINQ?

MybatisPlus Lambda表达式 聚合查询 分组查询 COUNT SUM AVG MIN MAX GroupBy

SQL多表连接