2014-10-08 51 views
0

我想用EF6做一個查詢,將急切加載一個相關的對象然後分組它然後計數相關記錄的總數並做分頁,但到目前爲止我不成功。實體框架選擇包含+分組計數+分頁和投影

這裏是我的嘗試,但它只能做一個連接和頁面

           _dbcontext.PublicUsers 
              .Include(x=> x.DocumentIndices) 
              .OrderBy(x=> x.Lastname) 
              .ThenBy(x=> x.Firstname) 
              .Skip((request.PageNumber - 1) * request.PageSize) 
              .Take(request.PageSize) 
              .ToList() 
              .Select(x => new PublicUserList 
               { 

                Id = x.Id, 
                Email = x.Email, 
                Firstname = x.Firstname, 
                Lastname = x.Lastname, 
                Middlename = x.Middlename, 
                Fullname = x.Fullname, 
                NoOfResults = x.DocumentIndices.Count() 
               }).ToList(); 

有沒有辦法做到這三個操作在一個單趟(加入+組由數+分頁)在EF6到數據庫?

+0

答案是肯定的。但是,這是我們可以用您提供的信息說的唯一一件事。 – 2014-10-08 19:44:56

回答

0

除非我誤讀了這一點,我認爲這是簡單地刪除幾行的問題:

_dbcontext.PublicUsers 
    .OrderBy(x=> x.Lastname) 
    .ThenBy(x=> x.Firstname) 
    .Skip((request.PageNumber - 1) * request.PageSize) 
    .Take(request.PageSize) 
    .Select(x => new PublicUserList 
    { 
     Id = x.Id, 
     Email = x.Email, 
     Firstname = x.Firstname, 
     Lastname = x.Lastname, 
     Middlename = x.Middlename, 
     Fullname = x.Fullname, 
     NoOfResults = x.DocumentIndices.Count() 
    }) 
    .ToList();