LINQ使用公共密钥加入两个字典 [英] Linq join two dictionaries using a common key
问题描述
我想基于一个共同的查找值将两个字典集合在一起
VAR IDLIST =新词典< INT ,INT>();
idList.Add(1,1);
idList.Add(3,3);
idList.Add(5,5);
变种lookupList =新词典< INT,INT>();
lookupList.Add(1,1000);
lookupList.Add(2,1001);
lookupList.Add(3,1002);
lookupList.Add(4,1003);
lookupList.Add(5,1004);
lookupList.Add(6,1005);
lookupList.Add(7,1006);
//事情是这样的:在idList.Keys
变种Q =从ID加入lookupList WTO对entry.Key等于ID
选择entry.Value;
上面的LINQ的说法只是一个例子,不进行编译。在IDLIST每个条目,拉从基于匹配键对lookupList值。
结果应该是从lookupList(1000,1002,1004)值的列表
什么是做到这一点使用LINQ的最简单的方法是什么?
感谢您,
里克
从idList.Keys ID
,其中lookupList.ContainsKey(ID)
让利值1 = IDLIST [ID]
设值2 = lookupList [ID]
选择新的{ID,值1,值}
或者,更经典
从kvp1在IDLIST
在lookupList加入kvp2上kvp1.Key等于kvp2.Key
选择新的{键= kvp1.Key,值1 = kvp1 .value的,值2 = kvp2.Value}
的
错在你的查询作用域之一:
从theBs一个在theAs
JOIN b上(leftside)等于(rightside)
一个是在leftside区域范围。 b为在rightside区域范围。
I am trying to join two Dictionary collections together based on a common lookup value.
var idList = new Dictionary<int, int>();
idList.Add(1, 1);
idList.Add(3, 3);
idList.Add(5, 5);
var lookupList = new Dictionary<int, int>();
lookupList.Add(1, 1000);
lookupList.Add(2, 1001);
lookupList.Add(3, 1002);
lookupList.Add(4, 1003);
lookupList.Add(5, 1004);
lookupList.Add(6, 1005);
lookupList.Add(7, 1006);
// Something like this:
var q = from id in idList.Keys
join entry in lookupList on entry.Key equals id
select entry.Value;
The Linq statement above is only an example and does not compile. For each entry in the idList, pull the value from the lookupList based on matching Keys.
The result should be a list of Values from lookupList (1000, 1002, 1004).
What’s the easiest way to do this using Linq?
Thank you,
Rick
from id in idList.Keys
where lookupList.ContainsKey(id)
let value1 = idList[id]
let value2 = lookupList[id]
select new {id, value1, value2}
Or, more classically
from kvp1 in idList
join kvp2 in lookupList on kvp1.Key equals kvp2.Key
select new {key = kvp1.Key, value1 = kvp1.Value, value2 = kvp2.Value}
The mistake in your query is one of scoping:
from a in theAs
join b in theBs on (leftside) equals (rightside)
a is in scope in the leftside area. b is in scope in the rightside area.
这篇关于LINQ使用公共密钥加入两个字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!