.include()影响查询的结果数

时间:2019-04-19 08:21:17

标签: c# entity-framework asp.net-core linq-to-sql entity-framework-core

我有一个非常简单的查询,它返回Order,但是当我对许多IncludesThenInclude执行相同的查询时,查询将返回0个结果而不是1个结果

var a = _context
        .Orders
        .FirstOrDefault(x => x.Id == OrderId);

var b = _context
        .Orders
        .OrdersFullInclude()
        .FirstOrDefault(x => x.Id == OrderId);

OrdersFullInclude() =

public static IQueryable<Order> OrdersFullInclude(this IQueryable<Order> input)
{
    return  input
           .Include(x => x.StatusesHistory)
           .Include(x => x.File)
           .Include(x => x.SomeData)
           .ThenInclude(x => x.SomeDeeperLevel)
           .Include(x => x.Company)
           .Include(x => x.Customer)
           .Include(x => x.Insurance)
           .ThenInclude(x => x.InsuranceSomething); 
}

为什么?

1 个答案:

答案 0 :(得分:1)

通过调用Include,您正在将结果与其他一些表联接,而且似乎并非所有联接的表都具有与第一个结果集中的记录相关的记录,因此它不返回任何结果。