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'的約束。
這是爲什麼?