C#-字典的交集<;内部,分类列表<;int,List<;int>&燃气轮机&燃气轮机;

C#-字典的交集<;内部,分类列表<;int,List<;int>&燃气轮机&燃气轮机;,c#,.net,linq,dictionary,intersection,C#,.net,Linq,Dictionary,Intersection,考虑以下代码: Dictionary<int, SortedList<int, List<int>>> ddl1 = new Dictionary<int, SortedList<int, List<int>>>(); ddl1.Add(1, new SortedList<int,List<int>>()); ddl1[1].Add(2, new List<int>()); ddl1[1

考虑以下代码:

Dictionary<int, SortedList<int, List<int>>> ddl1 = new Dictionary<int, SortedList<int, List<int>>>();

ddl1.Add(1, new SortedList<int,List<int>>());
ddl1[1].Add(2, new List<int>());
ddl1[1][2].Add(3);
ddl1[1][2].Add(4);
ddl1[1][2].Add(5);

ddl1[1].Add(3, new List<int>());
ddl1[1][3].Add(3);
ddl1[1][3].Add(4);
ddl1[1][3].Add(5);


ddl1.Add(2, new SortedList<int, List<int>>());
ddl1[2].Add(2, new List<int>());
ddl1[2][2].Add(3);
ddl1[2][2].Add(4);
ddl1[2][2].Add(5);


Dictionary<int, SortedList<int, List<int>>> ddl2 = 
         new Dictionary<int, SortedList<int, List<int>>>();

ddl2.Add(1, new SortedList<int, List<int>>());

ddl2[1].Add(3, new List<int>());
ddl2[1][3].Add(3);
ddl2[1][3].Add(4);



ddl2.Add(2, new SortedList<int, List<int>>());
ddl2[2].Add(2, new List<int>());
ddl2[2][2].Add(3);

有人能帮我解决这个问题吗?另外,它是否比手动的foreach和.contains方法更有效?提前谢谢

简而言之,最好用foreach手动完成。我想如果你真的想通过LINQ来实现这一点,你必须创建一些定制的
IEqualityComparer
s,而且这不会比手动的foreach解决方案少编码工作量

但是,如果您通过LINQ实现,您可能会从PLINQ中受益。除此之外,我不明白为什么LINQ解决方案可以更快

但是,您当然可以使用
Intersect
。但请记住,每次您按照注释中的建议调用ToDictionary时,您都会创建一个新的字典,这会耗费时间和内存

因此,假设您仍然需要
SortedList
,这是我能想到的最佳解决方案。如果没有,则用常用词典替换

var result = new Dictionary<int, SortedList<int, List<int>><();
foreach (var key1 in ddl1.Keys.Intersect(ddl2.Keys))
{
    var subList1 = ddl1[key1];
    var subList2 = ddl2[key1];
    var common1 = new SortedList<int, List<int>>();
    result.Add(key1, common1);
    foreach (var key2 in subList1.Keys.Intersect(subList2.Keys))
    {
        var subList1L2 = subList1[key2];
        var subList2L2 = subList2[key2];
        var common2 = subList1L2.Intersect(subList2L2).ToList();
        if (common2.Count > 0) common1.Add(key2, common2);
    }
}

var result=newdictionary简而言之,最好用foreach手动完成。我想如果你真的想通过LINQ来实现这一点,你必须创建一些定制的
IEqualityComparer
s,而且这不会比手动的foreach解决方案少编码工作量

但是,如果您通过LINQ实现,您可能会从PLINQ中受益。除此之外,我不明白为什么LINQ解决方案可以更快

但是,您当然可以使用
Intersect
。但请记住,每次您按照注释中的建议调用ToDictionary时,您都会创建一个新的字典,这会耗费时间和内存

因此,假设您仍然需要
SortedList
,这是我能想到的最佳解决方案。如果没有,则用常用词典替换

var result = new Dictionary<int, SortedList<int, List<int>><();
foreach (var key1 in ddl1.Keys.Intersect(ddl2.Keys))
{
    var subList1 = ddl1[key1];
    var subList2 = ddl2[key1];
    var common1 = new SortedList<int, List<int>>();
    result.Add(key1, common1);
    foreach (var key2 in subList1.Keys.Intersect(subList2.Keys))
    {
        var subList1L2 = subList1[key2];
        var subList2L2 = subList2[key2];
        var common2 = subList1L2.Intersect(subList2L2).ToList();
        if (common2.Count > 0) common1.Add(key2, common2);
    }
}

var result=newdictionary简而言之,最好用foreach手动完成。我想如果你真的想通过LINQ来实现这一点,你必须创建一些定制的
IEqualityComparer
s,而且这不会比手动的foreach解决方案少编码工作量

但是,如果您通过LINQ实现,您可能会从PLINQ中受益。除此之外,我不明白为什么LINQ解决方案可以更快

但是,您当然可以使用
Intersect
。但请记住,每次您按照注释中的建议调用ToDictionary时,您都会创建一个新的字典,这会耗费时间和内存

因此,假设您仍然需要
SortedList
,这是我能想到的最佳解决方案。如果没有,则用常用词典替换

var result = new Dictionary<int, SortedList<int, List<int>><();
foreach (var key1 in ddl1.Keys.Intersect(ddl2.Keys))
{
    var subList1 = ddl1[key1];
    var subList2 = ddl2[key1];
    var common1 = new SortedList<int, List<int>>();
    result.Add(key1, common1);
    foreach (var key2 in subList1.Keys.Intersect(subList2.Keys))
    {
        var subList1L2 = subList1[key2];
        var subList2L2 = subList2[key2];
        var common2 = subList1L2.Intersect(subList2L2).ToList();
        if (common2.Count > 0) common1.Add(key2, common2);
    }
}

var result=newdictionary简而言之,最好用foreach手动完成。我想如果你真的想通过LINQ来实现这一点,你必须创建一些定制的
IEqualityComparer
s,而且这不会比手动的foreach解决方案少编码工作量

但是,如果您通过LINQ实现,您可能会从PLINQ中受益。除此之外,我不明白为什么LINQ解决方案可以更快

但是,您当然可以使用
Intersect
。但请记住,每次您按照注释中的建议调用ToDictionary时,您都会创建一个新的字典,这会耗费时间和内存

因此,假设您仍然需要
SortedList
,这是我能想到的最佳解决方案。如果没有,则用常用词典替换

var result = new Dictionary<int, SortedList<int, List<int>><();
foreach (var key1 in ddl1.Keys.Intersect(ddl2.Keys))
{
    var subList1 = ddl1[key1];
    var subList2 = ddl2[key1];
    var common1 = new SortedList<int, List<int>>();
    result.Add(key1, common1);
    foreach (var key2 in subList1.Keys.Intersect(subList2.Keys))
    {
        var subList1L2 = subList1[key2];
        var subList2L2 = subList2[key2];
        var common2 = subList1L2.Intersect(subList2L2).ToList();
        if (common2.Count > 0) common1.Add(key2, common2);
    }
}

var result=newdictionary诀窍是在Dictionary键和SortedList键上连接两个字典:

var q1 = from d1 in ddl1
         from sl1 in d1.Value
         select new { d1.Key, sl1 };
var q2 = from d2 in ddl2
         from sl2 in d2.Value
         select new { d2.Key, sl2 };

var q = from ds1 in q1
        join ds2 in q2
        on         new { key1 = ds1.Key, key2 = ds1.sl1.Key } 
            equals new { key1 = ds2.Key, key2 = ds2.sl2.Key } 
        select new
        { 
            key1 = ds1.Key,
            key2 = ds1.sl1.Key,
            Intersect = ds1.sl1.Value.Intersect(ds2.sl2.Value)
        };
子查询(
q1、q2
)将字典展平为列表列表,因此对于join,可以组合字典键和列表键


现在,您可以对并行化是否确实提高了性能进行基准测试,首先使用
q2.aspallel()
,然后使用
q1.aspallel()

技巧是在字典键和SortedList键上连接两个字典:

var q1 = from d1 in ddl1
         from sl1 in d1.Value
         select new { d1.Key, sl1 };
var q2 = from d2 in ddl2
         from sl2 in d2.Value
         select new { d2.Key, sl2 };

var q = from ds1 in q1
        join ds2 in q2
        on         new { key1 = ds1.Key, key2 = ds1.sl1.Key } 
            equals new { key1 = ds2.Key, key2 = ds2.sl2.Key } 
        select new
        { 
            key1 = ds1.Key,
            key2 = ds1.sl1.Key,
            Intersect = ds1.sl1.Value.Intersect(ds2.sl2.Value)
        };
子查询(
q1、q2
)将字典展平为列表列表,因此对于join,可以组合字典键和列表键


现在,您可以对并行化是否确实提高了性能进行基准测试,首先使用
q2.aspallel()
,然后使用
q1.aspallel()

技巧是在字典键和SortedList键上连接两个字典:

var q1 = from d1 in ddl1
         from sl1 in d1.Value
         select new { d1.Key, sl1 };
var q2 = from d2 in ddl2
         from sl2 in d2.Value
         select new { d2.Key, sl2 };

var q = from ds1 in q1
        join ds2 in q2
        on         new { key1 = ds1.Key, key2 = ds1.sl1.Key } 
            equals new { key1 = ds2.Key, key2 = ds2.sl2.Key } 
        select new
        { 
            key1 = ds1.Key,
            key2 = ds1.sl1.Key,
            Intersect = ds1.sl1.Value.Intersect(ds2.sl2.Value)
        };
子查询(
q1、q2
)将字典展平为列表列表,因此对于join,可以组合字典键和列表键


现在,您可以对并行化是否确实提高了性能进行基准测试,首先使用
q2.aspallel()
,然后使用
q1.aspallel()

技巧是在字典键和SortedList键上连接两个字典:

var q1 = from d1 in ddl1
         from sl1 in d1.Value
         select new { d1.Key, sl1 };
var q2 = from d2 in ddl2
         from sl2 in d2.Value
         select new { d2.Key, sl2 };

var q = from ds1 in q1
        join ds2 in q2
        on         new { key1 = ds1.Key, key2 = ds1.sl1.Key } 
            equals new { key1 = ds2.Key, key2 = ds2.sl2.Key } 
        select new
        { 
            key1 = ds1.Key,
            key2 = ds1.sl1.Key,
            Intersect = ds1.sl1.Value.Intersect(ds2.sl2.Value)
        };
子查询(
q1、q2
)将字典展平为列表列表,因此对于join,可以组合字典键和列表键

现在,您可以对并行化是否确实提高了性能进行基准测试,首先使用
q2.AsParallel()
,然后使用
q1.AsParallel()

尝试以下方法:

public static IDictionary<int, SortedList<int, List<int>>> Intersect(IDictionary<int, SortedList<int, List<int>>> x, IDictionary<int, SortedList<int, List<int>>> y) {
    IDictionary<int, SortedList<int, List<int>>> one = x.Keys.Intersect(y.Keys).ToDictionary(k => k, k => x[k]);
    foreach (KeyValuePair<int, SortedList<int, List<int>>> kvp in one.ToList()) {
        one[kvp.Key] = new SortedList<int,List<int>>(kvp.Value.Keys.Intersect(y[kvp.Key].Keys).ToDictionary(k => k, k => y[kvp.Key][k]));
    }
    return one;
}
公共静态IDictionary Intersect(IDictionary x,IDictionary y){
IDictionary one=x.Keys.Intersect(y.Keys).ToDictionary(k=>k,k=>x[k]);
foreach(KeyValuePair kvp在一个.ToList()中){
一个[kvp.Key]=newsortedList(kvp.Value.Keys.Intersect(y[kvp.Key].Keys).ToDictionary(k=>k,k=>y[kvp.Key][k]);
}
返回一个;
}
尝试以下方法:

public static IDictionary<int, SortedList<int, List<int>>> Intersect(IDictionary<int, SortedList<int, List<int>>> x, IDictionary<int, SortedList<int, List<int>>> y) {
    IDictionary<int, SortedList<int, List<int>>> one = x.Keys.Intersect(y.Keys).ToDictionary(k => k, k => x[k]);
    foreach (KeyValuePair<int, SortedList<int, List<int>>> kvp in one.ToList()) {
        one[kvp.Key] = new SortedList<int,List<int>>(kvp.Value.Keys.Intersect(y[kvp.Key].Keys).ToDictionary(k => k, k => y[kvp.Key][k]));
    }
    return one;
}
公共静态IDictionary Intersect(IDictionary x,IDictionary y){
IDictionary one=x.Keys.Intersect(y.Keys).ToDictionary(k=>k,k=>x[k]);
foreach(KeyValuePair kvp在一个.ToList()中){
一个[kvp.Key]=newsortedList(kvp.Value.Keys.Intersect(y[kvp.Key].Keys).ToDictionary(k=>k,k=>y[kvp.Key][k]);
}
返回一个;
}
尝试以下方法:

public static IDictionary<int, SortedList<int, List<int>>> Intersect(IDictionary<int, SortedList<int, List<int>>> x, IDictionary<int, SortedList<int, List<int>>> y) {
    IDictionary<int, SortedList<int, List<int>>> one = x.Keys.Intersect(y.Keys).ToDictionary(k => k, k => x[k]);
    foreach (KeyValuePair<int, SortedList<int, List<int>>> kvp in one.ToList()) {
        one[kvp.Key] = new SortedList<int,List<int>>(kvp.Value.Keys.Intersect(y[kvp.Key].Keys).ToDictionary(k => k, k => y[kvp.Key][k]));
    }
    return one;
}
公共静态IDictionary Intersect(IDictionary x,IDictionary y){