How do you compare two dictionary keys using linq query? Below is the code I am currently using in my program:
foreach (KeyValuePair<string, string> sourceProject in sourceProjects)
{
foreach (KeyValuePair<string, string> targetProject in targetProjects)
{
if (targetProject.Key == sourceProject.Key)
{
// do something
}
}
}
I assume you want something like this:
from kv1 in sourceProjects
join kv2 in targetProjects on kv1.Key equals kv2.Key
select /* whatever, e. g. */ kv1.Value + kv2.Value
But actually more efficient would be this:
from key in sourceProjects.Keys.Intersect(targetProjects.Keys)
select /* whatever, e. g. */ sourceProjects[key] + targetProjects[key]
or maybe
from key in sourceProjects.Keys.Intersect(targetProjects.Keys)
let sourceProject = sourceProjects[key]
let targetProject = targetProjects[key]
select /* whatever, e. g. */ sourceProject + targetProject;
IEnumerable<KeyValuePair<string, string>> one;
IEnumerable<KeyValuePair<string, string>> two;
var duplicates = one.Select(o => o.Key).Where(k => two.Select(t => t.Key).Any(kTwo => kTwo == k));
foreach (var duplicateKey in duplicates)
{
// Do whatewer you want with those keys here
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.