2

我創建了一個MVC3應用程序,它使用WebGrid來填充數據庫中的數據。LINQ to Entities只支持投射EDM原語或枚舉類型

我的數據庫有一些列。說

Id | FullName | Phone | Email

我能排序Id列。但是,如果我嘗試其他的列進行排序,這是給下面的錯誤

無法轉換類型'MvcApp.Models.Student爲鍵入「MvcApp.Models.Student」。 LINQ to Entities僅支持投射EDM基元或枚舉類型。

我試圖調試,看看那裏的錯誤是,我發現排序是完美的,但是當它的數據返回到view提示錯誤

我查看代碼:

@{ 
    ViewBag.Title = "listStudents"; 
    Layout = "~/Views/Shared/_Layout.cshtml"; 
    WebGrid grid = new WebGrid(Model, canPage: true, canSort: true, rowsPerPage: 3); 
} 

@grid.Pager(WebGridPagerModes.NextPrevious) 
     @grid.GetHtml( //Error at this line 
     htmlAttributes: new { id = "grdListStudents" }, 
    fillEmptyRows: true, 
    headerStyle: "tblHeader", 
    tableStyle: "tablestyle", 
    mode: WebGridPagerModes.All, 
    firstText: "<< First", 
    previousText: "< Previous", nextText: "Next >", 
    lastText: "Last >>", 
    columns: new[]{ 
     grid.Column("intID","SId",canSort:true), 
     grid.Column("strFirstName","Name",canSort:true,format:(item)=>item.strFirstName+" "+item.strLastName), 
     grid.Column("strPhone","Phone",canSort:true), 
     grid.Column("strEmail","Email",canSort:true), 
    } 
    ) 

這是我在控制器代碼:接口IStudentInfo

public readonly IStudentInfo _istudentrepository; 

public studentController(IStudentInfo _iStudentRepository) 
{ 
    this._istudentrepository = _iStudentRepository; 
} 

public ActionResult listBidder(string sort, string sortdir, int? page) 
{ 
    int startPage = 0; 
    IEnumerable<Students> sList; 
    if (page.HasValue && page.Value > 0) 
    { 
     startPage = page.Value; 
    } 
    sList = _istudentrepository.GetList(startPage, PageSize, sort, sortdir); 
    return View(sList);     
} 

代碼:

public interface IStudentInfo 
{ 
    IEnumerable<Students> GetList(int intPage, int intRecords, string strSort, string sortdir);    
} 

守則型號:

private MyEntity _entity; 

public StudentListRepository(MyEntity Ent) 
{ 
    this._entity = Ent; 
} 
public IEnumerable<Students> GetList(int intPage, int intRecords, string strSort, string sortdir) 
{ 

    var finalresult = new Students(); 
    var bidList = (from userInfo in _entity.tbl_UserInf 
        join user in _entity.tbl_User on userInfo.UserId equals user.UserId 
        select new Students() 
        { 
         intID=user.UserId, 
         strFirstName = user.FirstName, 
         strEmail = userInfo.EmailId, 
         intPhone=userInfo.Phone 
        }).OrderByDescending(m => m.intID); 
    finalresult.TotalResult = bidList.Count(); 
    switch (strSort) 
    { 
     case "intID": 
      if (sortdir == "ASC") 
      { 
       sList = sList.OrderBy(r => r.Id); 
      } 
      else 
      { 
       sList= sList.OrderByDescending(r => r.Id); 
      } 
      break; 
     case "strFirstName": 
      if (sortdir == "ASC") 
      { 
       sList = sList.OrderBy(r => r.strFirstName); 
      } 
      else 
      { 
       sList= sList.OrderByDescending(r => r.strFirstName); 
      } 
      break; 
     case "strEmail": 
      if (sortdir == "ASC") 
      { 
       sList = sList.OrderBy(r => r.strEmail); 
      } 
      else 
      { 
       sList= sList.OrderByDescending(r => r.strEmail); 
      } 
      break; 
     //repeat same for phone 
    } 

    finalresult.lstStudents = sList.Skip(intPage * intRecords) 
            .Take(intRecords) 
            .ToList(); 
    return sList.AsEnumerable(); 
} 

什麼是錯在我的代碼?請告訴我,如果有人知道發生了什麼。

請幫

感謝,

+0

你能顯示控制器代碼? –

+0

請你能顯示有問題的linq請求的代碼嗎? _istudentrepository.GetList和所有的功能,直到你的實體? –

+0

完成!所有運行webgrid所需的代碼都在我的文章中編輯。 –

回答

2

嘗試使用:

public ActionResult listBidder(string sort, string sortdir, int? page) 
{ 
     int startPage = 0; 
     if (page.HasValue && page.Value > 0) 
     { 
      startPage = page.Value; 
     } 
     var sList = _istudentrepository.GetList(startPage, PageSize, sort, sortdir); 
     return View(sList); 
} 

public IEnumerable<Students> GetList(int intPage, int intRecords, string strSort, string sortdir) 
{ 

     var finalresult = new Students(); 
     var bidList = (from userInfo in _entity.tbl_UserInf 
         join user in _entity.tbl_User on userInfo.UserId equals user.UserId 
         select new Students() 
         { 
      intID=user.UserId, 
          strFirstName = user.FirstName, 
          strEmail = userInfo.EmailId, 
          intPhone=userInfo.Phone 
         }).OrderByDescending(m => m.intID); 
     finalresult.TotalResult = bidList.Count(); 
     // There are some sorting and ordering 
     finalresult.lstStudents = sList.Skip(intPage * intRecords).Take(intRecords).ToList(); 
     return bidList.ToArray(); 

}

+0

+1感謝百萬。它解決了我的問題。排序工作完美。 –

相關問題