在 C# 中创建一些列表联合
我有 3 个列表,其中每个列表都有以下类的一些实例:
public class Menu
{
public string Address { get; set; }
public string Name { get; set; }
public List<Menu> Childs=new List<Menu>();
}
我用下面的代码填充这些列表中的每个列表:
public List<Menu> GetAvailableMenus(string[] roles)
{
var adminMenu = new List<Menu>();
var terminalMenu = new List<Menu>();
var gaurdMenu = new List<Menu>();
if(roles.Contains("admin"))
{
GetAdminMenus(adminMenu);
}
if (roles.Contains("terminal"))
{
GetTerminalMenus(terminalMenu);
}
if (roles.Contains("gaurd"))
{
GetGaurdMenus(gaurdMenu);
}
return adminMenu.Union(gaurdMenu).Union(terminalMenu).ToList();
}
我的问题是联合操作只是连接这些列表,并且不合并它们以删除冗余项目。 有人知道我应该做什么吗?
I have 3 lists which every one of them has some instance of following class:
public class Menu
{
public string Address { get; set; }
public string Name { get; set; }
public List<Menu> Childs=new List<Menu>();
}
I fill everyone of these list in code below:
public List<Menu> GetAvailableMenus(string[] roles)
{
var adminMenu = new List<Menu>();
var terminalMenu = new List<Menu>();
var gaurdMenu = new List<Menu>();
if(roles.Contains("admin"))
{
GetAdminMenus(adminMenu);
}
if (roles.Contains("terminal"))
{
GetTerminalMenus(terminalMenu);
}
if (roles.Contains("gaurd"))
{
GetGaurdMenus(gaurdMenu);
}
return adminMenu.Union(gaurdMenu).Union(terminalMenu).ToList();
}
my problem is Union action just concat these list and do nor merge them in order to remove redundant items.
does anyone has any idea what should I do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您为 Union 提供相等比较器,那么它将能够合并而不仅仅是连接您的列表
请参阅 http://msdn.microsoft.com/en-us/library/bb358407.aspx
更新 - 再次阅读您的问题我想知道您是否也希望合并子菜单项。如果是这种情况,那么我认为您需要编写一些逻辑来执行此操作 - 我认为没有任何方法可以使用 Linq Union 运算符来执行此嵌套合并。
If you provide an equality comparer for Union then it will be able to merge and not just concat your lists
See http://msdn.microsoft.com/en-us/library/bb358407.aspx
Update - reading your question again I'm wondering if you also want the child menu items merged too. If that's the case, then I think you will need to write some logic to do this - I don't think there's any way of using the Linq Union operator to do this nested merging.
您的
Menu
类需要正确实现IEquatable
才能确定两个条目是否相同。否则,Union
将使用默认相等性,这将比较引用相等性。Your
Menu
class needs to properly implementIEquatable<>
in order to determine whether two entries are the same or not. OtherwiseUnion
will use the default equality, which will compare reference equality.您还没有为
Menu
类重写Equals
或GetHashCode
,因此Union
没有办法(引用相等除外) ,这在这里不起作用)以知道存在重复项。以合理的方式实现这些方法将使您的代码正常工作。这些链接可能会有所帮助:
或者,您可以将自己的
IEqualityComparer
实现作为另一个参数传递给 联合方法。You haven't overridden
Equals
orGetHashCode
for yourMenu
class, soUnion
has no way (except reference equality, which won't work here) to know that there are duplicates. Implementing those methods in a sensible way will make your code work.These links might help:
Alternatively, you can just pass in your own implementation of
IEqualityComparer
as another argument to the Union method.