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