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

c# – 不能比较EF查询中的元素异常

发布时间:2020-12-15 04:00:42 所属栏目:百科 来源:网络整理
导读:我基本上是: public ActionResult MyAction(Listint myIds){ var myList = from entry in db.Entries where (myIds == null || myIds.Contains(entry.Id)) select entry; return View(myList);} 目的是仅获取带有传递ID的项目或返回所有这些项目. (其他标准
我基本上是:
public ActionResult MyAction(List<int> myIds)
{
    var myList = from entry in db.Entries
             where (myIds == null || myIds.Contains(entry.Id))
                 select entry;
    return View(myList);
}

目的是仅获取带有传递ID的项目或返回所有这些项目. (其他标准为了清楚而剪除)

当我返回myList时,我收到异常,我已经做了一些调试,并且发生在.ToList()

Cannot compare elements of type ‘System.Collections.Generic.List`1’.
Only primitive types (such as Int32,String,and Guid) and entity
types are supported.

解决方法

问题是因为myIds是空的.

我需要:

public ActionResult MyAction(List<int> myIds)
{
    if(myIds == null)
    {
        myIds = new List<int>();    
    }
    bool ignoreIds = !myIds.Any();

    var myList = from entry in db.Entries
                 where (ignoreIds || myIds.Contains(entry.Id))
                 select entry;
    return View(myList);
}

(编辑:李大同)

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

    推荐文章
      热点阅读