linq 如何缩短动态链接中的连接层

l2osamch  于 2023-01-28  发布在  其他
关注(0)|答案(2)|浏览(132)

每当添加连接时,都会添加一个新图层,这很难实现,尤其是在where动态子句中如何用一个词来概括每个连接我放置一个查询来显示查询我的意思是,我在这里使用了多个连接,每个Gwen都添加了t我希望添加或查询的每个字段都必须添加一个长字符串t我希望直接写入您为连接命名的名称

context.Trips
    .GroupJoin(context.Entities, trips => trips.BeneficiaryEntitiesId, beneficiarys => beneficiarys.Id,
        (trips, tripsEntities) => new { trips, tripsEntities })
    .SelectMany(t => t.tripsEntities.DefaultIfEmpty(),
        (t, beneficiarys) => new { t, beneficiarys })
    .GroupJoin(context.Items, t => t.t.trips.ItemId, items => items.Id,
        (t, tripsItems) => new { t, tripsItems })
    .SelectMany(t => t.tripsItems.DefaultIfEmpty(),
        (t, items) => new { t, items })
    .Join(context.Entities, t => t.t.t.t.trips.CarrierEntitiesId,
        carriers => carriers.Id,
        (t, carriers) => new { t, carriers })
    .Join(context.Vehicl, t => t.t.t.t.t.trips.VehiclId,
        vehicls => vehicls.Id,
        (t, vehicls) => new { t, vehicls })
    .Join(context.DefineLists, t => t.vehicls.VehiclType,
        vehiclsType => vehiclsType.Id,
        (t, vehiclsType) => new { t, vehiclsType })
    .Join(context.Drivers, t => t.t.t.t.t.t.t.trips.EntryDriverId,
         endrivers => endrivers.Id,
        (t, endrivers) => new { t, endrivers })
    .GroupJoin(context.Drivers, t => t.t.t.t.t.t.t.t.trips.OutputDriverId,
        oudrivers => oudrivers.Id,
        (t, oudrivers) => new { t, oudrivers })
    .SelectMany(t => t.oudrivers.DefaultIfEmpty(),
        (t, oudrivers) => new { t, oudrivers })
    .Where(@$"(t.t.t.vehiclsType.ListType==1 {SDateWhere}{EDateWhere})
                      {SubWhere}{MainWhere}", StartDate, ed)
    .Select(s => new
    {
        s.t.t.t.t.t.t.t.t.t.trips.Id,
        TripsFullNo = $"{s.t.t.t.t.t.t.t.t.t.trips.TripsFullNo}   {s.t.t.t.t.t.t.t.t.t.trips.TripsNo}",
        OperationTtype =
            s.t.t.t.t.t.t.t.t.t.trips.Loading
                ? operationTtypes[s.t.t.t.t.t.t.t.t.t.trips.OperationTtype ?? 0]
                : $"{Language.ListOperateTransfer} {Language.ListEmpt}",
        Vehicl = $"{s.t.t.t.t.vehicls.LicensePlate}/{s.t.t.t.vehiclsType.ListItem}",
        CarrierEntities = s.t.t.t.t.t.carriers.EntitieName,
        BeneficiaryEntities = s.t.t.t.t.t.t.t.t.beneficiarys!.EntitieName,
        Item = s.t.t.t.t.t.t.items!.NoName,
        Driver = $"{s.t.t.endrivers.DriverLicense}/{s.t.t.endrivers.DriverName}"
    }).OrderByDescending(o=>o.Id)
hs1ihplo

hs1ihplo1#

下面是一个在创建多个连接时将深度转换为宽度的示例。请注意,正确使用EF/EF核心可以使嵌套连接变得不必要,并且更容易使用。

var test = db.GroupJoin(context.Entities,
                 trips => trips.BeneficiaryEntitiesId, beneficiarys => beneficiarys.Id,
                (trips, tripEntities) => new { trips, tripEntities })
            .SelectMany(
                 tb => tb.tripEntities.DefaultIfEmpty(),
                (tb, beneficiaries) => new { tb.trips, beneficiaries })
            .GroupJoin(context.Items,
                 tb => tb.trips.ItemId, items => items.Id,
                (tb, tripsItems) => new { tb.trips, tb.beneficiaries, tripsItems })
            .SelectMany(
                 tbi => tbi.tripsItems.DefaultIfEmpty(),
                (tbi, items) => new { tbi.trips, tbi.beneficiaries, items })
            .Join(context.Entities,
                 tbi => tbi.trips.CarrierEntitiesId, carriers => carriers.Id,
                (tbi, carriers) => new { tbi.trips, tbi.beneficiaries, tbi.items, carriers })
            .Join(context.Vehicl,
                 tbic => tbic.trips.VehiclId, vehicls => vehicls.Id,
                (tbic, vehicls) => new { tbic.trips, tbic.beneficiaries, tbic.items, tbic.carriers, vehicls })
            .Join(context.DefineLists,
                 tbicv => tbicv.vehicls.VehiclType, vehiclsType => vehiclsType.Id,
                (tbicv, vehiclsType) => new { tbicv.trips, tbicv.beneficiaries, tbicv.items, tbicv.carriers, tbicv.vehicls, vehiclsType })
            .Join(context.Drivers,
                 tbicvv => tbicvv.trips.EntryDriverId, endrivers => endrivers.Id,
                (tbicvv, endrivers) => new { tbicvv.trips, tbicvv.beneficiaries, tbicvv.items, tbicvv.carriers, tbicvv.vehicls, tbicvv.vehiclsType, endrivers })
            .GroupJoin(context.Drivers,
                 tbicvve => tbicvve.trips.OutputDriverId, oudrivers => oudrivers.Id,
                (tbicvve, oudrivers) => new { tbicvve.trips, tbicvve.beneficiaries, tbicvve.items, tbicvve.carriers, tbicvve.vehicls, tbicvve.vehiclsType, tbicvve.endrivers, oudrivers })
            .SelectMany(
                 tbicvveo => tbicvveo.oudrivers.DefaultIfEmpty(),
                (tbicvveo, oudrivers) => new { tbicvveo.trips, tbicvveo.beneficiaries, tbicvveo.items, tbicvveo.carriers, tbicvveo.vehicls, tbicvveo.vehiclsType, tbicvveo.endrivers, oudrivers })
            .Where(@$"(trips.vehiclsType.ListType==1 {SDateWhere}{EDateWhere}) {SubWhere}{MainWhere}", StartDate, ed)
            .Select(tbcvvo => new {
                tbcvvo.trips.Id,
                TripsFullNo = $"{tbcvvo.trips.TripsFullNo}   {tbcvvo.trips.TripsNo}",
                OperationTtype =
                    tbcvvo.trips.Loading
                        ? operationTtypes[tbcvvo.trips.OperationTtype ?? 0]
                        : $"{Language.ListOperateTransfer} {Language.ListEmpt}",
                Vehicl = $"{tbcvvo.vehicls.LicensePlate}/{tbcvvo.vehiclsType.ListItem}",
                CarrierEntities = tbcvvo.carriers.EntitieName,
                BeneficiaryEntities = tbcvvo.beneficiaries!.EntitieName,
                Item = tbcvvo.items!.NoName,
                Driver = $"{tbcvvo.endrivers.DriverLicense}/{tbcvvo.endrivers.DriverName}"
            })
            .OrderByDescending(o=>o.Id)
kx5bkwkv

kx5bkwkv2#

通过创建一个扩展方法来表示LeftJoin(这需要对结果使用已知的类),您可以将GroupJoinSelectMany操作与一些次要的命名问题结合起来。
给定:

public class AnswerClass<TOuter, TInner> {
    public TOuter O;
    public TInner I;
}

public static class IQueryableExt {
    public static IQueryable<AnswerClass<TOuter, TInner>> LeftJoin<TOuter, TInner, TKey>(this IQueryable<TOuter> outer,
        IQueryable<TInner> inner, Expression<Func<TOuter,TKey>> outerKeyFn, Expression<Func<TInner, TKey>> innerKeyFn)
        => outer.GroupJoin(inner, outerKeyFn, innerKeyFn, (outer, inners) => new { outer, inners })
                .SelectMany(oi => oi.inners.DefaultIfEmpty(), (oi, i) => new AnswerClass<TOuter, TInner> { O = oi.outer, I = i });
}

var test = db.LeftJoin(context.Entities, trips => trips.BeneficiaryEntitiesId, beneficiaries => beneficiaries.Id)
            .LeftJoin(context.Items, oi => oi.O.ItemId, items => items.Id)
            .Join(context.Entities,
                 ooi => ooi.O.O.CarrierEntitiesId, carriers => carriers.Id,
                (ooi, carriers) => new { trips = ooi.O.O, beneficiary = ooi.O.I, items = ooi.I, carriers })
            .Join(context.Vehicl,
                 tbic => tbic.trips.VehiclId, vehicls => vehicls.Id,
                (tbic, vehicls) => new { tbic.trips, tbic.beneficiary, tbic.items, tbic.carriers, vehicls })
            .Join(context.DefineLists,
                 tbicv => tbicv.vehicls.VehiclType, vehiclsType => vehiclsType.Id,
                (tbicv, vehiclsType) => new { tbicv.trips, tbicv.beneficiary, tbicv.items, tbicv.carriers, tbicv.vehicls, vehiclsType })
            .Join(context.Drivers,
                 tbicvv => tbicvv.trips.EntryDriverId, endrivers => endrivers.Id,
                (tbicvv, endrivers) => new { tbicvv.trips, tbicvv.beneficiary, tbicvv.items, tbicvv.carriers, tbicvv.vehicls, tbicvv.vehiclsType, endrivers })
            .LeftJoin(context.Drivers, tbicvve => tbicvve.trips.OutputDriverId, oudrivers => oudrivers.Id)
            // Remap LeftJoin result to something with better naming
            .Select(oi => new { oi.O.trips, oi.O.beneficiary, oi.O.items, oi.O.carriers, oi.O.vehicls, oi.O.vehiclsType, oi.O.endrivers, ouDrivers = oi.I })
            //.Where(@$"(trips.vehiclsType.ListType==1 {SDateWhere}{EDateWhere}) {SubWhere}{MainWhere}", StartDate, ed)
            .Select(tbcvvo => new {
                tbcvvo.trips.Id,
                TripsFullNo = $"{tbcvvo.trips.TripsFullNo}   {tbcvvo.trips.TripsNo}",
                OperationTtype =
                    tbcvvo.trips.Loading
                        ? operationTtypes[tbcvvo.trips.OperationTtype ?? 0]
                        : $"{Language.ListOperateTransfer} {Language.ListEmpt}",
                Vehicl = $"{tbcvvo.vehicls.LicensePlate}/{tbcvvo.vehiclsType.ListItem}",
                CarrierEntities = tbcvvo.carriers.EntitieName,
                BeneficiaryEntities = tbcvvo.beneficiary!.EntitieName,
                Item = tbcvvo.items!.NoName,
                Driver = $"{tbcvvo.endrivers.DriverLicense}/{tbcvvo.endrivers.DriverName}"
            })
            .OrderByDescending(o=>o.Id);

相关问题