0

Consider the below code

private List<Orders> GetOrders(int id) 
{
  var result = Context.Orders.Where(x => x.OrderId == 15)
                    .Include(x => x.Products)
                    .Include(x => x.CustomerDetails)
                    .ToList();
  return result;
}

Products and CustomerDetails are Navigation properties on Orders Entity. I just want to get results with details about the most expensive product only.

So I tried below,

Context.Orders.Where(x => x.OrderId == 15)
                    .Include(x => x.Products.OrderByDescending(p => p.Price).Take(1))
                    .Include(x => x.CustomerDetails)
                    .ToList();

But this is resulting in an error,

An exception of type 'System.ArgumentException' occurred in EntityFramework.dll but was not handled in user code Additional information: 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.

How to resolve this issue?

user1928158
  • 173
  • 1
  • 9

1 Answers1

0

According to this answer, it is not possible. Perhaps the best answer to your specific problem is to order the products after loading.

Community
  • 1
  • 1
Neil
  • 11,059
  • 3
  • 31
  • 56
  • or indeed the answer below it, where it shows how to do it !! I must read past the first answer next time... – Neil Jun 21 '16 at 11:13