1

我只想从数据库中获取 1 个带有 3 张发票的客户。下面说明的代码没有给我 3 个结果(但引发异常)。

例外:包含路径表达式必须引用在类型上定义的导航属性。对引用导航属性使用虚线路径,对集合导航属性使用 Select 运算符。参数名称:路径

一个客户可以有多个发票,一个发票可以有多个 InvoiceLines。

我想用 3 个发票和相关的 InvoiceLines 检索一个客户。

我在这里做错了什么?

    public async Task<Client> Client(int id)
    {
        using (var db = GetContext())
        {                
                return await db.Client.Include(x => x.Invoices.Take(3))
                .Where(i => !i.IsDeleted)
                .Include(c => c.Invoices.Select(x => x.InvoiceLines))
                .Where(x => x.Id == id)
                .FirstOrDefaultAsync();              
        }
    }
4

1 回答 1

0

我不认为.Include可以用来过滤记录。

我没有足够的代码来验证这是否符合您的要求,但我认为您可以在不使用.Include的情况下做您想做的事。

var client = await db.Client
    .Where(i => !i.IsDeleted)
    .FirstOrDefaultAsync(x => x.Id == id);

var top3Invoices = client.Invoices.Take(3);
于 2015-03-26T18:18:41.350 回答