c# – Entity Framework 5.0.我的查询有什么问题?

前端之家收集整理的这篇文章主要介绍了c# – Entity Framework 5.0.我的查询有什么问题?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我的代码
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;
}

如果我调用contractStartDate.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
> EF 5.0
> databaseContext是System.Data.Entity.DbContext的子代

解决方法

我知道这个错误.只需确保partnerNumbers不为null.您为此参数传递了null值,但Linq-to-entities无法将该值转换为任何有意义的值.
if (partnerNumbers == null)
{
    throw new ArgumentNullException("partnerNumbers");
}

额外的奖励建议:

如果SomeDateTime不可为空并且枚举中没有条目,那么在调用Min()时会出现异常.将SomeDateTime转换为查询中的可空类型将起作用,然后在没有条目时获得null.

猜你在找的C#相关文章