与实体框架结合的Linq内部查询,查询错误

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

我正在尝试模仿此SQL查询,以得到预期的结果:

enter image description here

在Linq查询中,但出现一些错误。这是我的Linq翻译:

              var customerAircraftVM = await (
          from ca in _context.CustomerAircrafts
          join ac in _context.Aircrafts on ca.AircraftId equals ac.AircraftId
          into leftJoinCustomerAircrafts
          from b in leftJoinCustomerAircrafts.DefaultIfEmpty()
             join aprice in (from ap in _context.AircraftPrices
                          join pt in _context.PricingTemplate
                          on ap.PriceTemplateId equals pt.Oid
                          where pt.Fboid == fboId
                          select new
                          {
                              CustomerAircraftId = ap.CustomerAircraftId,
                              Name = pt.Name
                          })
                          on ca.Oid equals aprice.CustomerAircraftId into pricesgpt
                          from a in pricesgpt.DefaultIfEmpty()
          where ca.GroupId.GetValueOrDefault() == groupId && ca.CustomerId == customerId 
          select new CustomerAircraftsGridViewModel
          {
              Oid = ca.Oid,
              GroupId = ca.GroupId.GetValueOrDefault(),
              AircraftId = ca.AircraftId,
              PricingTemplateName = p == null ? "" : p.Name,
          });

我出错了

对象引用未设置为对象的实例。

我在做什么错?

entity-framework linq linq-to-sql entity-framework-core linq-to-entities
1个回答
0
投票

您是内部联接的左联接结果。我认为这是您的C#代码中遗漏的。但这不是例外。

您可以尝试这样。

var result = 
from ca in _dbcontext.CustomerAircrafts
//// here join the inner join
join pta in (from f in _dbcontext.AircraftPrices
            join pt in _dbcontext.PricingTemplate on ap.PriceTemplateId equals pt.Oid
            where pt.Fboid == fboId
            select new { pt, ap.CustomerAircraftId })
on ca.Oid equals pta.CustomerAircraftId into fcmt
where ca.GroupId == groupid 
from m in fcmt.DefaultIfEmpty() //// this will do the left join
select new
{
  // what you want
};
© www.soinside.com 2019 - 2024. All rights reserved.