【问题标题】:Why does selecting a custom model result in a violation of type constraints?为什么选择自定义模型会导致违反类型约束?
【发布时间】:2017-10-15 07:13:52
【问题描述】:

鉴于此模型:

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; }
    }

}

我通过以下方式选择它:

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:

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

当我访问我得到的页面时:

方法 System.Web.UI.WebControls.QueryableHelpers.CountHelper:类型 参数“X.ViewModels.YRow”违反了类型参数的约束 'T'。

这是为什么呢?

【问题讨论】:

    标签: c# asp.net entity-framework webforms


    【解决方案1】:

    问题是由于类 YRow 被定义为 struct 引起的。将其更改为 class 会使错误消失。还有其他问题,但这个问题已经解决了。

    【讨论】:

      猜你喜欢
      • 2015-07-13
      • 2017-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-30
      相关资源
      最近更新 更多