如何将linq groupjoin转换为lambda groupjoin

时间:2018-04-12 03:12:35

标签: c# linq lambda

因为我需要做一些过滤条件功能,我现在想要将linq查询转换为lambda表达式对象。

linq代码:

var query = from chlsynclog in oaPtDbContext.TableChlSyncLog

                        join mealchl in oaPtDbContext.TableMealChl on new { X1 = chlsynclog.Mealid, Y1 = chlsynclog.Chid } equals new { X1 = mealchl.Mealid, Y1 = mealchl.Chid }
                        into mealchlGroup
                        from mealchlGroupItem in mealchlGroup.DefaultIfEmpty()

                        join service in oaPtDbContext.TableService on mealchlGroupItem.Sid equals service.Sid
                        into serviceGroup
                        from serviceGroupItem in serviceGroup.DefaultIfEmpty()

                        join channel in oaPtDbContext.TableChannel on chlsynclog.Chid equals channel.Chid
                        into channelGroup
                        from channelGroupItem in channelGroup.DefaultIfEmpty()

                        join area in oaPtDbContext.TableArea on chlsynclog.Areaid equals area.Areaid
                        into areaGroup
                        from areaGroupItem in areaGroup.DefaultIfEmpty()

                        select new
                        {
                            chlsynclog.Id,
                            chlsynclog.Handset,
                            mealchlGroupItem.Mealname,
                            areaGroupItem.Proname,
                            areaGroupItem.Cityname,
                            chlsynclogType= GetChlsynclogType(chlsynclog.Type),
                            statusName=GetStatusName(chlsynclog.Statusid),
                            channelGroupItem.Chname,
                            syncTime=chlsynclog.Synctime.ToString("yyyy-MM-dd HH:mm:ss")
                        };
在我翻译成第三个

后,我开始做这项工作
  

加入

var testQuery =

                oaPtDbContext.TableChlSyncLog

                .GroupJoin(oaPtDbContext.TableMealChl,
                (chlsynclog) => new
                {
                    X1 = chlsynclog.Mealid,
                    X2 = chlsynclog.Chid
                },
                (mealchl) => new
                {
                    X1 = mealchl.Mealid,
                    X2 = mealchl.Chid
                },
                (x, y) => new
                {
                    X = x,
                    Y = y
                })
                .SelectMany(temp0 => temp0.Y.DefaultIfEmpty())

                .GroupJoin(oaPtDbContext.TableService,
                mealchl => mealchl.Sid,
                service => service.Sid,
                (x, y) => new { X = x, Y = y })
                .SelectMany(temp0 => temp0.Y.DefaultIfEmpty())

                .GroupJoin(oaPtDbContext.TableChannel,)

                ;
  

.GroupJoin(oaPtDbContext.TableChannel,)

第二个参数只是获取前一个类型的对象TableService, 但我需要的参数应该是TableCholSynclog的Chid。 所以在这里我不知道继续这项工作。 我使用query.Expression.ToString()来查看表达式:

<>h__TransparentIdentifier3.<>h__TransparentIdentifier2.<>h__TransparentIdentifier1.<>h__TransparentIdentifier0.chlsynclog.Chid

源代码可以做到这一点,但我不能!

那么,如何编写我的lambda表达式????

1 个答案:

答案 0 :(得分:0)

你确定要lambda吗?

var query = oaPtDbContext.TableChlSyncLog.GroupJoin(
                              oaPtDbContext.TableMealChl,
                              chlsynclog => new
                                 {
                                    X1 = chlsynclog.Mealid,
                                    Y1 = chlsynclog.Chid
                                 },
                              mealchl => new
                                 {
                                    X1 = mealchl.Mealid,
                                    Y1 = mealchl.Chid
                                 },
                              (chlsynclog, mealchlGroup) => new
                                 {
                                    chlsynclog,
                                    mealchlGroup
                                 })
                           .SelectMany(
                              @t => mealchlGroup.DefaultIfEmpty(),
                              (@t, mealchlGroupItem) => new
                                 {
                                    @t,
                                    mealchlGroupItem
                                 })
                           .GroupJoin(
                              oaPtDbContext.TableService,
                              @t => mealchlGroupItem.Sid,
                              service => service.Sid,
                              (@t, serviceGroup) => new
                                 {
                                    @t,
                                    serviceGroup
                                 })
                           .SelectMany(
                              @t => serviceGroup.DefaultIfEmpty(),
                              (@t, serviceGroupItem) => new
                                 {
                                    @t,
                                    serviceGroupItem
                                 })
                           .GroupJoin(
                              oaPtDbContext.TableChannel,
                              @t => chlsynclog.Chid,
                              channel => channel.Chid,
                              (@t, channelGroup) => new
                                 {
                                    @t,
                                    channelGroup
                                 })
                           .SelectMany(
                              @t => channelGroup.DefaultIfEmpty(),
                              (@t, channelGroupItem) => new
                                 {
                                    @t,
                                    channelGroupItem
                                 })
                           .GroupJoin(
                              oaPtDbContext.TableArea,
                              @t => chlsynclog.Areaid,
                              area => area.Areaid,
                              (@t, areaGroup) => new
                                 {
                                    @t,
                                    areaGroup
                                 })
                           .SelectMany(
                              @t => areaGroup.DefaultIfEmpty(),
                              (@t, areaGroupItem) => new
                                 {
                                    chlsynclog.Id,
                                    chlsynclog.Handset,
                                    mealchlGroupItem.Mealname,
                                    areaGroupItem.Proname,
                                    areaGroupItem.Cityname,
                                    chlsynclogType = GetChlsynclogType(chlsynclog.Type),
                                    statusName = GetStatusName(chlsynclog.Statusid),
                                    channelGroupItem.Chname,
                                    syncTime = chlsynclog.Synctime.ToString("yyyy-MM-dd HH:mm:ss")
                                 });

注意:这只是Resharpers,Convert Linq to chain method功能。你可以把它变得更漂亮。

相关问题