【问题标题】:Comparing Two Lists比较两个列表
【发布时间】:2011-06-19 21:34:43
【问题描述】:

我有 2 个列表。我应该比较这两个列表,并且应该从两个列表中获取公共值,我应该把它放在一个字符串数组中......

第一个列表....

List<string> IDs = new List<string>();
Dictionary<string, Test> group = TestProxy.GetNames(IDs.ToArray());

第二个列表...

List<string> Names = new List<string>();
Dictionary<string, Test> groupNames = TestProxy.GetSections(Names.ToArray());

如何取共同的价值观...

更新代码....

        List<string> IDs = new List<string>();
        modulesIDs.Add("ID1");
        Dictionary<string, Group> group = PrivilegeProxy.GetNames(IDs.ToArray());

        List<string> Roles = new List<string>();
        Roles.Add("Role1");
        Dictionary<string, Role> Role = PrivilegeProxy.GetRoles(Roles.ToArray());


        Dictionary<string, "******"> common = group.Intersect(Role).ToDictionary(x => x.Key, x => x.Value);

        return common;

在“通用”词典中我应该给出哪个对象(“通用词典”)

【问题讨论】:

  • 哇,朋友……这改变了问题的整个范围……

标签: c# linq asp.net-mvc-2


【解决方案1】:

使用Enumerable.IntersectEnumerable.ToDictionary

    Dictionary<string, Test> commonGroups = group.Intersect(groupNames)
        .ToDictionary(x=>x.Key, x => x.Value);

更新:如果仍有疑问,请阅读此内容。 Recreating a Dictionary from an IEnumerable

【讨论】:

  • 我应该在 ...x=>x.Key, x => x.Value 中给出什么
  • 无需更改任何内容。只需按原样使用代码。如果您有错误,请返回
猜你喜欢
  • 2011-02-15
  • 2021-04-15
  • 1970-01-01
  • 2013-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多