5

这是我的代码:

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;
}

如果我调用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.

我的查询有什么问题?

  • contractsStartDate类型 System.Data.Entity.Infrastructure.DbQuery

  • 英孚 5.0

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

4

1 回答 1

3

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

if (partnerNumbers == null)
{
    throw new ArgumentNullException("partnerNumbers");
}

额外的奖励建议:

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

于 2013-03-12T14:05:39.417 回答