2017-05-16 33 views
1

鑑於這種模式:爲什麼選擇自定義模型會導致違反類型約束?

public IQueryable<YRow> FetchData() 
{ 
    return db.Ys 
     .Select(x => new YRow 
     { 
      Y = x, 
      LastPhoneCall = x.Z 
       .Where(y => y.Type == YZType.PhoneCall) 
       .OrderBy(y => y.Date).LastOrDefault(), 
      LastVisit = x.Z 
       .Where(y => y.Type == YZType.Visit) 
       .OrderBy(y => y.Date).LastOrDefault(), 
      LastStatus = x.Statuses.OrderBy(y => y.Date).LastOrDefault(), 

     }) 
     .OrderBy(x => x.Y.BusinessName); 
} 

被傳遞到一個asp:GridView的SelectMethod:

using X.Models; 

namespace X.ViewModels 
{ 

    public struct YRow 
    { 
     public Y Y { get; set; } 
     public YContact LastPhoneCall { get; set; } 
     public YContact LastVisit { get; set; } 
     public YStatus LastStatus { get; set; } 
    } 

} 

我通過選擇它

<asp:GridView runat="server" SelectMethod="FetchData" ItemType="X.ViewModels.YRow"> 
    ... 
</asp:GridView> 

當我訪問該網頁,我得到:

方法系統tem.Web.UI.WebControls.QueryableHelpers.CountHelper:類型 參數'X.ViewModels.YRow'違反了類型參數 'T'的約束。

這是爲什麼?

回答

0

該問題是由類YRow定義爲struct引起的。將其更改爲class使錯誤消失。還有其他問題,但這個問題解決了。

相關問題