我在 foreach 循环中出现指向 in
的错误!?以前从未发生过。这可能是什么原因?我错过了什么吗?
错误信息:
An exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll but was not handled in user code
Additional information: Unable to create a constant value of type 'System.Object'. Only primitive types or enumeration types are supported in this context.
我的代码:
List<int> WeeksInProject = new List<int>();
var w = from x in db.Activities
where x.ProjectID.Equals(1)
select x;
foreach (var wNum in w)
{
WeeksInProject.Add(wNum.WeekNumber);
}
最佳答案
var w = from x in db.Activities
where x.ProjectID.Equals(1) select x;
应该是:
var w = from x in db.Activities
where x.ProjectID == 1 select x;
如果ProjectID
是int?
,
var w = from x in db.Activities where
x.ProjectID.HasValue &&
x.ProjectID.Value == 1 select x;
关于c# - Foreach 循环错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37183042/