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

  • databaseContextSystem.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。