C# 在linq中使用contains
我有一个过滤列表,它返回MenuTable中的所有区别C# 在linq中使用contains,c#,linq,contains,C#,Linq,Contains,我有一个过滤列表,它返回MenuTable中的所有区别 var _parentList = _employee.Designation.Role.MenuRoles .Select(x => new {
var _parentList = _employee.Designation.Role.MenuRoles
.Select(x => new
{
MenuParentID = x.Menu.ParentID
})
.DistinctBy(x => x.MenuParentID)
.OrderBy(x => x.MenuParentID)
.ToList();
我想从\u parentList
这就是我尝试过的,并且在\u parentList.Contains(x.Id)
上出现了一个错误,该错误表示System.Generic.Contains的最佳重载匹配具有一些无效参数。
MenuParentList = _db.Menus.Where(x => _parentList.Contains(x.Id))
.Select(x => new SMS.Models.ViewModel.DashboardVM.MenuParent
{
MenuParentID = x.Id,
MenuParentName = x.MenuName
})
.ToList()
任何帮助都将不胜感激。Cf.此代码:
.Select(x => new
{
MenuParentID = x.Menu.ParentID
})
这将生成一个匿名对象列表,其中包含一个名为MenuParentID的属性,而不是整数列表。编译器为您创建的类型在结构上如下所示(请注意,编译器会在幕后生成一个不可用的类名,而不是AnonymousType1
,但您知道了):
而
\u parentList
将属于List类型。问题出在哪里?您的问题是什么?我看不出您的代码中有任何问题。它应该可以工作。\u parentList
不仅仅包含Id
,它是一个不同类型的列表,您试图在其中找出Id
,x.Id
的类型与\u parentList
中的元素类型不同
class AnonymousType1
{
public int MenuParentID {get;set;}
}
var _parentList = _employee.Designation.Role.MenuRoles
.Select(x => x.Menu.ParentID)
.Distinct()
.OrderBy(id => id)
.ToList();