我想得到所有的車輛,但只有每輛車的最新報告,而不是所有的卡片和所有報告。一輛車可以有多個報告。如何只用Include()獲取第一個元素?
// with this i would get all cars with all reports for each car.
// How can i get all cars with only the last report for each car?
context.Cars.Include("Reports");
我想是這樣的:
context.Cars.Include(c => c.Reports.OrderByDescending(r => r.Id).FirstOrDefault())
.Take(10)
.ToList();
但這並沒有工作。
我認爲編寫'Include()'函數的人不太可能認爲這個優化值得實施。使用兩個查詢。 –
使用'.Select(...)'而不是 –
@GeneR會要求太多的例子嗎?即使是更抽象的人也會這樣做。 –