我正在MVC4中创建一个应用程序。我的模型如下:
public class NonComplianceData
{
public static List<MonthDefinitions> Months = new List<MonthDefinitions>()
{
new MonthDefinitions {Month = "January", Order = 1},
new MonthDefinitions {Month = "February", Order = 2},
new MonthDefinitions {Month = "March", Order = 3},
new MonthDefinitions {Month = "April", Order = 4},
new MonthDefinitions {Month = "May", Order = 5},
new MonthDefinitions {Month = "June", Order = 6},
new MonthDefinitions {Month = "July", Order = 7},
new MonthDefinitions {Month = "August", Order = 8},
new MonthDefinitions {Month = "September", Order = 9},
new MonthDefinitions {Month = "October", Order = 10},
new MonthDefinitions {Month = "November", Order = 11},
new MonthDefinitions {Month = "December", Order = 12},
};
public int InspectorId { get; set; }
public string InspectorName { get; set; }
public IEnumerable<MonthData> FullYearData { get; set; }
}
public class MonthDefinitions
{
public string Month { get; set; }
public int Order { get; set; }
}
public class MonthData
{
public string Month { get; set; }
public int TotalAuditsCompleted { get; set; }
public int TotalNoDefects { get; set; }
public decimal NonComplianceRate
{
get
{
if (TotalAuditsCompleted == 0)
{
return 0;
}
else
{
return (TotalNoDefects / (decimal)TotalAuditsCompleted) * 100;
}
}
}
}
我正在尝试使用以下内容填充数据:
var inspectorData =
context.COESDetails
.Where(x =>
x.UploadCOESDetails.AuditZoneId == criteria.AuditZoneId &&
x.UploadCOESDetails.AuditMonth.Contains(criteria.AuditYear))
.Select(x => x.Inspector)
.Where(y => y.Id != 0)
.Distinct()
.OrderBy(x => x.Firstname)
.Select(ud => new NonComplianceData
{
InspectorId = ud.Id,
InspectorName = ud.Firstname + " " + ud.Surname,
FullYearData =
NonComplianceData.Months.Select(month =>
new MonthData
{
Month = month.Month,
})
});
我收到以下错误
base {System.SystemException} = {“无法创建常量值 输入'AIS.Domain.Models.MonthDefinitions'。只有原始类型或 在此上下文中支持枚举类型。“}
任何帮助将不胜感激..谢谢
附加说明:
如果我使用此
,问题就解决了public static IEnumerable<string> Months =
new string[] { "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December" };
但我需要确保正确分类月份,这就是我添加订单属性的原因
答案 0 :(得分:0)
给出您的错误消息:
{&#34;无法创建类型&#39; AIS.Domain.Models.MonthDefinitions&#39;的常量值。在此上下文中仅支持原始类型或枚举类型。&#34;}
我会说您应该在select子句中添加toList()
,因为您正在使用的ORM(可能是实体框架?)无法将您的复杂对象Months
转换为正确的查询。 / p>