我的NavigationItem
有导航属性NavigationItemsPermissions
,我有RoleId
。作为函数中的输入参数,我有List<int> roleIds
,其中我将有类似{1,3}的东西。
如何在输入列表中完成LINQ
给我回复NavigationItem
个NavigationItemsPermissions
某些 RoleId
。 注意,此外,我只希望输入列表中包含NavigationItemsPermissions
的{{1}}。
这就是我坚持
的地方RoleId
如果有开发者列表
,则给出一个更简单的类比(不是真实数据)public List<NavigationItem> GetNavigationItems(MenuType menuType, List<int> roleIds)
{
var navigationItems = DbContext.NavigationItems.Where(x => x.MenuTypeId == (int) menuType && !x.IsDeleted && x.NavigationItemsPermissions.Any(r=>r.RoleId **in roleIds**)));
return navigationItems;
}
在给定的技能列表developers: [
{ name: "Hickory", skills: ["c#", "js", "linq"] }
{ name: "Dickory", skills: ["html", "css", "js"] }
{ name: "Dock", skills: ["html", "c#", "oracle"] }
]
中,我希望得到以下结果
list = [ "c#", "linq" ]
真实数据样本:
感谢您的帮助。
答案 0 :(得分:3)
在这种情况下你可以使用contains
public List<NavigationItem> GetNavigationItems(MenuType menuType, List<int> roleIds)
{
var navigationItems = DbContext.NavigationItems.Where(x => x.MenuTypeId == (int) menuType && !x.IsDeleted && roleIds.contains(x.RoleId)));
return navigationItems;
}
答案 1 :(得分:3)
这应该会给你想要的结果
public List<NavigationItem> GetNavigationItems(MenuType menuType, List<int> roleIds)
{
var navigationItems = DbContext.NavigationItems
.Where(x => x.MenuTypeId == (int) menuType &&
!x.IsDeleted &&
x.NavigationItemsPermissions.Any(r => roleIds.Contains(r.RoleId)));
return navigationItems;
}
修改强>
如果你&#34;只需要NavigationItemsPermissions,其中NavigationItemsPermissions.RoleId在roleIds&#34; 中,你可以使用这个查询
var navigationPermissions = DbContext.NavigationItems
.Where(x => x.MenuTypeId == (int) menuType &&
!x.IsDeleted)
.SelectMany(ni => ni.NavigationItemsPermissions)
.Where(np => roleIds.Contains(np.RoleId))
.ToList();
如果有重复的项目且您只想要不同的项目,请在Distinct()
之前使用ToList()
。
修改2
var navigationItems = DbContext.NavigationItems
.Where(x => x.MenuTypeId == (int) menuType &&
!x.IsDeleted &&
x.NavigationItemsPermissions.Any(r => roleIds.Contains(r.RoleId)));
navigationItems.ForEach(x => x.NavigationItemsPermissions = x.NavigationItemsPermissions.Where(r => roleIds.Contains(r.RoleId)).ToList());