猿问

如何将复杂的 linq 映射到对象

寻求有关 Ef Core 和 Linq 的帮助。假设我提出了一个很大的要求,以接收所有包含有关产品、公司等信息的支持票。这很简单,只需加入一些东西:


select * 

from Tickets T

left join Products P on T.ProductId = P.Id                  

left join ProductVersions PV on T.ProductVersionId = PV.Id  

left join TicketTypes TT on T.TicketTypeId = TT.Id          

left join TicketPriorities TP on T.TicketPriorityId = TP.Id 

left join TicketStates TS on T.TicketStateId = TS.Id    


left join AbpTenants A on T.TenantId = A.Id                 

    left join AbpEditions E on A.EditionId = E.Id   


left join TicketLinkedUsers TLU on TLU.TicketId = T.Id      

    left join TicketLinkTypes TLT on TLT.Id = TLU.TicketLinkTypeId  

但是我对最后 4 个连接有问题。


在项目中,我使用的是 Ef Core。这就是我制作它的方式(部分):


var query = (from o in filteredTickets

                     join o1 in _productRepository.GetAll() on o.ProductId equals o1.Id into j1

                     from s1 in j1.DefaultIfEmpty()

                     join o2 in _productVersionRepository.GetAll() on o.ProductVersionId equals o2.Id into j2

                     from s2 in j2.DefaultIfEmpty()

                     join o3 in _ticketTypeRepository.GetAll() on o.TicketTypeId equals o3.Id into j3

                     from s3 in j3.DefaultIfEmpty()

                     join o4 in _ticketPriorityRepository.GetAll() on o.TicketPriorityId equals o4.Id into j4

                     from s4 in j4.DefaultIfEmpty()

                     join o5 in _ticketStateRepository.GetAll() on o.TicketStateId equals o5.Id into j5

                     from s5 in j5.DefaultIfEmpty()

                     join o6 in _tenantManager.Tenants on o.TenantId equals o6.Id into j6

                     from s6 in j6.DefaultIfEmpty()

                     })

为了接收数据,我使用了存储库模式。然后使用 AutoMapper 将所有数据映射到 ViewModel。这是我的 ViewModel 的样子:


现在,我尝试通过有关版本(一对多关系)的信息和 TicketLinkedUsers 列表(多对多关系)以及 TicketLinkType 信息获取有关公司(AbpTenants)的信息。架构:

白衣染霜花
浏览 141回答 1
1回答

千万里不及你

按照提示,我重写了一切询问:var query = filteredTickets // IQueryable<Ticket>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.Include(ten => ten.Tenant)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .ThenInclude(ed => ed.Edition)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.Include(p => p.Product)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.Include(pv => pv.ProductVersion)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.Include(tt => tt.TicketType)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.Include(tp => tp.TicketPriority)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.Include(ts => ts.TicketState)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.Include(lu => lu.LinkedUsers)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .ThenInclude(tlt => tlt.TicketLinkType)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.Include(lu => lu.LinkedUsers)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .ThenInclude(u => u.User)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .ProjectTo<GetTicketForView>();映射:configuration.CreateMap<Ticket, TicketDto>();configuration.CreateMap<Tenant, TenantShortInfoDto>();configuration.CreateMap<TicketState, TicketStateTableDto>();configuration.CreateMap<TicketLinkedUser, TicketLinkedUserDto>();configuration.CreateMap<Ticket, GetTicketForView>()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .ForMember(dest => dest.Ticket, conf => conf.MapFrom(src => src));如果我将来想切换到 nhibernate,不确定我是否在这里失去了一些灵活性,但它比以前好得多。
随时随地看视频慕课网APP
我要回答