0

我一直在遵循this很好的演練使用MVC貢獻創建一個網格。MVC Contrib網格失敗.OrderBy()與NHibernate異常

但是我的版本使用NHibernate,而不是LINQ到實體。

該代碼工作正常的排序點。

public ActionResult Index(string clientName, int? countryId, GridSortOptions gridSortOptions, int? page) 
    { 
     var clientList = from c in this.ClientRepository.Query 
         select new ListedClientViewModel(c); 

     //Set default sort column 
     if (string.IsNullOrWhiteSpace(gridSortOptions.Column)) 
      gridSortOptions.Column = "ClientName"; 

     // Filter on clientName 
     if (!string.IsNullOrWhiteSpace(clientName)) 
      clientList = clientList.Where(c => c.ClientName.StartsWith(clientName)); 

     // Filter on country 
     if (countryId.HasValue) 
      clientList = clientList.Where(c => c.CountryId == countryId); 

     // Order and page the clients 
     var clientPageList = clientList 
      //Sorting causes error. 
      //.OrderBy(gridSortOptions.Column, gridSortOptions.Direction) 
      .AsPagination(page ?? 1, 10); 

     var clientListContainer = new ClientListContainerViewModel 
     { 
      ClientPageList = clientPageList, 
      GridSortOptions = gridSortOptions 
     }; 

     return View(clientListContainer); 
    } 

如果我取消對該行.OrderBy(gridSortOptions.Column, gridSortOptions.Direction)當它擊中與System.NotSupportedException {"NewExpression"}

任何想法認爲我怎麼能解決這個問題,它會失敗?

非常感謝,Kohan。

回答

0

解決了它......只需在過濾和排序應用後稍後投射視圖模型。

var clientList = this.ClientRepository.Query; 

    ... 
    ... 

    // Order and page the clients 
    var clientPageList = clientList 
     //Sorting no longer causes error. 
     .OrderBy(gridSortOptions.Column, gridSortOptions.Direction) 
     .Select(c => new ListedClientViewModel(c)) 
     .AsPagination(page ?? 1, 10);