在Linq Lambda表达式中连接更多3个表时如何避免复杂性

时间:2019-05-17 09:58:29

标签: c# linq join lambda

来自SQL背景我习惯于在单个查询中联接5-8个表。 我无法想象使用Linq lambda表达式语法执行相同的操作。 在这里,我将加入4-5个表/集合。

EG:

var viewmodel = logs.Join(CargoElements.lstContainerLoadStatus.ToList(), l => l.LoadStatus, ls => ls.Value, (x, ls) =>
                   new
                   {
                       log = x,
                       LoadStatusDesc = ls.Text
                   })
                   .Join(db.CargoContainerSize, log => log.log.CargoContainerSizeID, ccsize => ccsize.Id, (log, ccsize) => new
                   {
                       log = log,
                       CargoContainerSizeIDDesc = ccsize.Size
                   }).Join(db.CargoContainerType, log => log.log.log.CargoContainerTypeID, cct => cct.Id, (log, cct) => new
                   {
                       log = log,
                       CargoContainerTypeIDDesc = cct.Name
                   }).GroupJoin(db.CargoFrom, log => log.log.log.log.CargoFromID, cf => cf.ID, (log, cf) => new
                   {
                       log = log,
                       cf = cf
                   }).SelectMany(temp => temp.cf.DefaultIfEmpty(), (temp, cf) => new
                   {
                       log = temp,
                       CargoFromIDDesc = cf.Description
                   }
                   )
                   .Select(x => new ContainerInLogsVM
                   {

                       ContainerInLogID = x.log.log.log.log.log.ContainerInLogID,
                       ContainerInID = x.log.log.log.log.log.ContainerInID,
                       CargoID = x.log.log.log.log.log.CargoID,
                       LoadStatus = x.log.log.log.log.log.LoadStatus,
                       LoadStatusDesc = x.log.log.log.log.LoadStatusDesc,
                       Shipper = x.log.log.log.log.log.Shipper,
                       CnFAgentName = x.log.log.log.log.log.CnFAgentName,
                       ShippingBill = x.log.log.log.log.log.ShippingBill,
                       ContainerNo = x.log.log.log.log.log.ContainerNo,
                       CargoContainerSizeID = x.log.log.log.log.log.CargoContainerSizeID,
                       CargoContainerSizeIDDesc = x.log.log.log.CargoContainerSizeIDDesc,
                       CargoContainerTypeID = x.log.log.log.log.log.CargoContainerTypeID,
                       CargoContainerTypeIDDesc = x.log.log.CargoContainerTypeIDDesc,
                       OtherType = x.log.log.log.log.log.OtherType,
                       VesselNo = x.log.log.log.log.log.VesselNo,
                       ShipperBillNo = x.log.log.log.log.log.ShipperBillNo,
                       SealOTLNo = x.log.log.log.log.log.SealOTLNo,
                       VoyageNo = x.log.log.log.log.log.VoyageNo,
                       BLNumber = x.log.log.log.log.log.BLNumber,
                       Purpose = x.log.log.log.log.log.Purpose,
                       CargoFromID = x.log.log.log.log.log.CargoFromID,
                       CargoFromIDDesc = x.CargoFromIDDesc,
                       OtherFrom = x.log.log.log.log.log.OtherFrom,
                       Status = x.log.log.log.log.log.Status,
                       Remark = x.log.log.log.log.log.Remark,
                       StatusChangedOn = x.log.log.log.log.log.StatusChangedOn,
                       StatusChangedBy = x.log.log.log.log.log.StatusChangedBy,
                       StatusChangedByDesc = "",
                       SysRemark = x.log.log.log.log.log.SysRemark
                   }

                   ).ToList();

这太荒谬了!还是我做错了? LINQ中必须有更好的方法。 我最好在lambda表达式中寻找答案。

0 个答案:

没有答案