1

I want to take only 1 client with 3 invoices from the DB. The code stated below does not give me 3 results(but throws an exception).

Exception: The Include path expression must refer to a navigation property defined on the type. Use dotted paths for reference navigation properties and the Select operator for collection navigation properties. Parameter name: path

There is one Client which can have multiple Invoices, one Invoice which can have multiple InvoiceLines.

I want to retrieve one Client with 3 Invoices and the associated InvoiceLines.

What did i do wrong here?

    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();              
        }
    }
Lars
  • 736
  • 1
  • 8
  • 18

1 Answers1

0

I don't think .Include can be used to filter out records.

I don't have enough code to verify this does what you want, but I think you can do what you want without using .Include at all.

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

var top3Invoices = client.Invoices.Take(3);
recursive
  • 83,943
  • 34
  • 151
  • 241
  • I've edited my first post, this is not exactly what i meant. Now you can see the return type of the method. – Lars Mar 26 '15 at 20:27
  • Hm. If a client has more than 3 invoices, then I don't think you can make EF act like it has any less. – recursive Mar 26 '15 at 20:46