如果我理解正確的話,你不希望每頁顯示10名的博客,但已在10天內創建的所有博客。然後,你可以嘗試此查詢:
int page = 1; // the page you want to display, 1, 2, 3, ...
int daysPerPage = 10;
List<Blog> result = blogs
.GroupBy(b => b.Created.Date) // group blogs by day
.OrderByDescending(g => g.Key) // sort the day groups descending by day
.Skip((page - 1) * daysPerPage) // skip the pages before requested page
.Take(daysPerPage) // take the next 10 day groups
.SelectMany(g => g) // get all blogs of 10 days in flat list
.OrderByDescending(b => b.Created) // sort blogs descending by blog date+time
.ToList(); // make a list with the result
所顯示的最早日期可以發現:
DateTime? oldestDate = result
.Select(b => (DateTime?)b.Created.Date)
.LastOrDefault();
(可空的東西是隻能在這裏對付那result
是空的情況下oldestDate
會null
即可。)
如果您要使用這個數據庫查詢,即blogs
實際上是一個DbSet<Blog>
像context.Blogs
,你可能要重寫它AB因爲DateTime.Date
不支持使用LINQ到實體:
List<Blog> result = context.Blogs
.GroupBy(b => new { b.Created.Year, b.Created.Month, b.Created.Day })
.OrderByDescending(g => g.Key.Year)
.ThenByDescending(g => g.Key.Month)
.ThenByDescending(g => g.Key.Day)
.Skip((page - 1) * daysPerPage)
.Take(daysPerPage)
.SelectMany(g => g)
.OrderByDescending(b => b.Created)
.ToList();
只是爲了澄清這是10個結果我正在尋找10天的價值。而且,它不一定非得是10個結果。也許10應該是最低金額。我會每天做每一頁,但一天可能沒有任何結果,或者可能有很多。謝謝。 – AndrewPolland