码迷,mamicode.com
首页 > 其他好文 > 详细

Lambda表达式多表连接的左连

时间:2019-11-07 13:22:12      阅读:364      评论:0      收藏:0      [点我收藏+]

标签:highlight   lambda表达式   ==   border   使用   完整   count   ice   mod   

  在网上有很多人都在查找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表达式多表连接的左连

标签:highlight   lambda表达式   ==   border   使用   完整   count   ice   mod   

原文地址:https://www.cnblogs.com/zunzunQ/p/11811285.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!