加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 05:37:11 所属栏目:百科 来源:网络整理
导读:这是我的代码: public DateTime GibSomeStartDate(IEnumerableint partnerNumbers,DateTime startTime){ var contractsStartDate = from contract in this.databaseContext.Contract where partnerNumbers.Contains(contract.Pnr) contract.SomeDateTime =
这是我的代码:
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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读