我在以下代码中遇到以下错误,该如何解决? [英] I am getting the following error with the following code, how to fix?
问题描述
我正在MVC4中创建一个应用程序.我的模型如下:
I am creating an application in MVC4. My model is as follows:
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;
}
}
}
}
我正在尝试使用以下数据填充数据:
I am trying to fill the data with the following:
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".仅原始类型或 在此上下文中支持枚举类型.}
base {System.SystemException} = {"Unable to create a constant value of type 'AIS.Domain.Models.MonthDefinitions'. Only primitive types or enumeration types are supported in this context."}
任何帮助将不胜感激..谢谢
Any help would be appreciated.. thanks
附加说明:
如果我使用此问题,则此问题已解决
The problem is fixed if I use this
public static IEnumerable<string> Months =
new string[] { "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December" };
但是我需要确保月份正确排序,这就是为什么我添加了order属性
but I need to ensure that the months are sorted properly, that's why I added an order property
推荐答案
给出您的错误消息:
{无法创建类型为'AIS.Domain.Models.MonthDefinitions'的常量值.在此上下文中仅支持基本类型或枚举类型.}
{"Unable to create a constant value of type 'AIS.Domain.Models.MonthDefinitions'. Only primitive types or enumeration types are supported in this context."}
我会说您应该在选择子句中添加toList()
,因为您正在使用的ORM(可能是实体框架?)无法将复杂的对象Months
转换为正确的查询.
I would said that you should add toList()
in your select clause since the ORM you're using (probably Entity Framework?) cannot convert your complex object Months
to a correct query.
这篇关于我在以下代码中遇到以下错误,该如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!