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

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...