Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C中使用linq查询比较两个字典键_C#_.net - Fatal编程技术网

C# 在C中使用linq查询比较两个字典键

C# 在C中使用linq查询比较两个字典键,c#,.net,C#,.net,如何使用linq查询比较两个字典键?以下是我目前在程序中使用的代码: foreach (KeyValuePair<string, string> sourceProject in sourceProjects) { foreach (KeyValuePair<string, string> targetProject in targetProjects) { if (targetProject.Key == sourceProject.Ke

如何使用linq查询比较两个字典键?以下是我目前在程序中使用的代码:

foreach (KeyValuePair<string, string> sourceProject in sourceProjects)
{
    foreach (KeyValuePair<string, string> targetProject in targetProjects)
    {
        if (targetProject.Key == sourceProject.Key)
        {
            // do something
        }
    }
}

我想你想要这样的东西:

from kv1 in sourceProjects
join kv2 in targetProjects on kv1.Key equals kv2.Key
select /* whatever, e. g. */ kv1.Value + kv2.Value
但实际上更有效的方法是:

from key in sourceProjects.Keys.Intersect(targetProjects.Keys)
select /* whatever, e. g. */ sourceProjects[key] + targetProjects[key]
或许

from key in sourceProjects.Keys.Intersect(targetProjects.Keys)
let sourceProject = sourceProjects[key]
let targetProject = targetProjects[key]
select /* whatever, e. g. */ sourceProject + targetProject;

我想你想要这样的东西:

from kv1 in sourceProjects
join kv2 in targetProjects on kv1.Key equals kv2.Key
select /* whatever, e. g. */ kv1.Value + kv2.Value
但实际上更有效的方法是:

from key in sourceProjects.Keys.Intersect(targetProjects.Keys)
select /* whatever, e. g. */ sourceProjects[key] + targetProjects[key]
或许

from key in sourceProjects.Keys.Intersect(targetProjects.Keys)
let sourceProject = sourceProjects[key]
let targetProject = targetProjects[key]
select /* whatever, e. g. */ sourceProject + targetProject;

为什么需要使用Linq,这段代码有什么问题?Linq可能会为您“节省”一行代码,但就分配和性能而言,它几乎总是效率较低。快速而肮脏的解决方案是:sourceProjects.Keys.IntersecttargetProjects.Keys,但它的作用基本相同-迭代两个键。你想干什么?多久一次?您可以使用HashSet或SortedSet来加速操作,但您仍然需要付费从原始字典加载HashSet或SortedSet。更好的一个线性行是sourceProjects.Keys.Wherekey=>target.ContainsKeykey。这仍然会在源代码上进行迭代,不像xxxSet类利用密钥散列。我之所以要使用linq查询,是因为我希望尽可能优化代码。此外,我计划使用类似的方法来比较每个项目中的文件夹。我打赌项目的遍历不是瓶颈。此外,使用LINQ通常不会提高执行时间,相反,使用LINQ通常会降低代码的执行速度。通过使用字典键查找是O1这一事实,您可以获得一些优化。为什么需要使用Linq,这段代码有什么问题?Linq可能会为您“节省”一行代码,但就分配和性能而言,它几乎总是效率较低。快速而肮脏的解决方案是:sourceProjects.Keys.IntersecttargetProjects.Keys,但它的作用基本相同-迭代两个键。你想干什么?多久一次?您可以使用HashSet或SortedSet来加速操作,但您仍然需要付费从原始字典加载HashSet或SortedSet。更好的一个线性行是sourceProjects.Keys.Wherekey=>target.ContainsKeykey。这仍然会在源代码上进行迭代,不像xxxSet类利用密钥散列。我之所以要使用linq查询,是因为我希望尽可能优化代码。此外,我计划使用类似的方法来比较每个项目中的文件夹。我打赌项目的遍历不是瓶颈。此外,使用LINQ通常不会提高执行时间,相反,使用LINQ通常会降低代码的执行速度。通过使用字典键查找是O1这一事实,您可以获得一些优化。