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

问题描述 投票:5回答:1

寻找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()
                     // join o7 in _editionaRepository.GetAll() on s6.EditionId equals o7.Id into j7
                     // from s7 in j7.DefaultIfEmpty()
                     // join o8 in _ticketLinkedUsersRepository.GetAll() on o.Id equals o8.TicketId into j8
                     // from s8 in j8.DefaultIfEmpty()
                     // join o9 in _ticketLinkTypesRepository.GetAll() on s9.TicketLinkTypeId equals o9.Id into j9
                     // from s9 in j9.DefaultIfEmpty()
                     select new GetTicketForView() { Ticket = ObjectMapper.Map<TicketDto>(o)
                        , ProductName = s1 == null ? "" : s1.Name.ToString()
                        , ProductVersionName = s2 == null ? "" : s2.Name.ToString()
                        , TicketTypeName = s3 == null ? "" : s3.Name.ToString()
                        , TicketPriorityName = s4 == null ? "" : s4.Name.ToString()
                        , TicketState = ObjectMapper.Map<TicketStateTableDto>(s5)
                        , Tenant = ObjectMapper.Map<TenantShortInfoDto>(s6)
                     })

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

public class GetTicketForView
{
    public TicketDto Ticket { get; set; }

    public TenantShortInfoDto Tenant { get; set; }

    public string ProductName { get; set;}

    public string ProductVersionName { get; set;}

    public TicketStateTableDto TicketState { get; set; }

    public string TicketTypeName { get; set;}

    public string TicketPriorityName { get; set;}

    public List<TicketLinkedUserDto> LinkedUsers { get; set; }
}

现在,我尝试使用TicketLinkType信息获取有关公司(AbpTenants)的信息,其中包含有关版本(一对多关系)和TicketLinkedUsers列表(多对多关系)的信息。架构:enter image description here

我可以使用其他连接接收所有必需的数据,但是我不知道如何正确绑定数据并将数据映射到GetTicketForView。具有嵌套映射版本的LinkedUser和Tenant列表是一个问题。现在我正在为每张票做单独的请求以进行多对多的工作:

// execute to get tickets
tickets = await query
           .OrderBy(input.Sorting ?? "ticket.id asc")
           .PageBy(input)
           .ToListAsync();

// then for each ticket get related users:
foreach (var ticket in tickets)
{
    var linkedUsers = _ticketLinkedUsersRepository
   .GetAllIncluding(lu => lu.TicketLinkType, lu => lu.User)
   .OrderBy(a => a.TicketLinkType.Ordinal)
   .Where(p => p.TicketId == ticket.Ticket.Id).ToList();

   ticket.LinkedUsers = ObjectMapper.Map<List<TicketLinkedUserDto>>(linkedUsers);
}

这是一个非常耗时的原因,因为我可以使用o8和09在一个请求中获取所有数据,但我为每个票证提出了额外的请求。没有足够的经验来做对。

所以问题是,如何使用linq实现第一个请求并将其映射到ViewModel。我应该使用额外的请求吗?或者最好使用Ef Core Api来提出复杂的请求?或者不可能在linq中映射复杂的东西?

提前致谢

c# entity-framework linq .net-core automapper
1个回答
0
投票

按照提示,我重写了一切

查询:

var query = filteredTickets // IQueryable<Ticket>
             .Include(ten => ten.Tenant)
                .ThenInclude(ed => ed.Edition)
             .Include(p => p.Product)
             .Include(pv => pv.ProductVersion)
             .Include(tt => tt.TicketType)
             .Include(tp => tp.TicketPriority)
             .Include(ts => ts.TicketState)
             .Include(lu => lu.LinkedUsers)
                .ThenInclude(tlt => tlt.TicketLinkType)
             .Include(lu => lu.LinkedUsers)
                .ThenInclude(u => u.User)
            .ProjectTo<GetTicketForView>();

映射:

configuration.CreateMap<Ticket, TicketDto>();
configuration.CreateMap<Tenant, TenantShortInfoDto>();
configuration.CreateMap<TicketState, TicketStateTableDto>();
configuration.CreateMap<TicketLinkedUser, TicketLinkedUserDto>();

configuration.CreateMap<Ticket, GetTicketForView>()
            .ForMember(dest => dest.Ticket, conf => conf.MapFrom(src => src)
);

如果我想在将来切换到nhibernate,不确定我是否在这里失去了一些灵活性,但它比以前更好用。

© www.soinside.com 2019 - 2024. All rights reserved.