entity framework5.0。 我的查询有什么问题?
这是我的代码:
public DateTime GibSomeStartDate(IEnumerable partnerNumbers, DateTime startTime) { var contractsStartDate = from contract in this.databaseContext.Contract where partnerNumbers.Contains(contract.Pnr) && contract.SomeDateTime >= startTime select contract.SomeDateTime; }
如果我调用contractsStartDate.Min()
,则会发生exception:
Unable to create a null constant value of type 'System.Collections.Generic.IEnumerable`1'. Only entity types, enumeration types or primitive types are supported in this context.
我的查询有什么问题?
-
contractsStartDate
的类型为System.Data.Entity.Infrastructure.DbQuery
-
EF 5.0
-
databaseContext
是System.Data.Entity.DbContext
子代
我知道这个错误。 只需确保partnerNumbers
不为null。 您为此参数传递了null值,但Linq-to-entities无法将该值转换为任何有意义的值。
if (partnerNumbers == null) { throw new ArgumentNullException("partnerNumbers"); }
额外的奖励建议:
如果SomeDateTime
not nullable
并且枚举中没有条目,那么在调用Min()
会出现exception。 将SomeDateTime
转换为查询中的nullable
类型将起作用,然后在没有条目时SomeDateTime
null。