实体框架5.0.我的查询有什么问题?

Mik*_*Del 5 c# linq entity-framework

这是我的代码:

public DateTime GibSomeStartDate(IEnumerable<int> partnerNumbers, DateTime startTime)
{
     var contractsStartDate = from contract in this.databaseContext.Contract
                              where partnerNumbers.Contains(contract.Pnr) 
                                 && contract.SomeDateTime >= startTime
                              select contract.SomeDateTime;
}
Run Code Online (Sandbox Code Playgroud)

如果我调用contractsStartDate.Min()异常发生:

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.
Run Code Online (Sandbox Code Playgroud)

我的查询有什么问题?

  • 所述contractsStartDate的类型为 System.Data.Entity.Infrastructure.DbQuery

  • EF 5.0

  • databaseContext 是的孩子 System.Data.Entity.DbContext

Jus*_*now 3

我知道这个错误。只要确保partnerNumbers不为空即可。您为此参数传递空值,但 Linq-to-entities 无法将该值转换为任何有意义的值。

if (partnerNumbers == null)
{
    throw new ArgumentNullException("partnerNumbers");
}
Run Code Online (Sandbox Code Playgroud)

额外的奖励建议:

如果SomeDateTimenot nullable并且枚举中没有条目,那么您将在调用时遇到异常Min()。转换SomeDateTimenullable查询中的类型将起作用,然后当没有条目时您将得到 null。