微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我基本上是:
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);
}

原文地址:https://www.jb51.cc/csharp/93026.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐