我有词典收藏
{key -> string, value -> class}
我还有另一个字典集
{key -> string, value -> string}
注意:第二个字典集合{key -> string, value -> string}.Value
是第一个字典集合{key -> string, value -> class}.Key
所以,我必须根据第二个收集价值从第一个字典集中找到所有数据。
答案 0 :(得分:1)
class MyTest
{
public int myValue { get; set; }
}
Main()
{
Dictionary<string, string> First = new Dictionary<string, string>();
First.Add("dd", "test");
First.Add("ss", "test");
First.Add("tt", "test");
First.Add("aa", "test");
First.Add("mm", "test");
Dictionary<string, MyTest> Second = new Dictionary<string, MyTest>();
Second.Add("dd", new MyTest() { myValue = 123 });
Second.Add("oo", new MyTest() { myValue = 123 });
Second.Add("tt", new MyTest() { myValue = 123 });
Second.Add("aa", new MyTest() { myValue = 123 });
Second.Add("rr", new MyTest() { myValue = 123 });
var Final1 = First.Where(S => Second.Any(T => S.Key.Equals(T.Key)));
var Final2 = Second.Where(S => First.Any(T => S.Key.Equals(T.Key)));
Console.WriteLine("\nFirst\n");
foreach (var item in Final1)
{
Console.WriteLine(item.Key + "-" + item.Value);
}
Console.WriteLine("\nSecond\n");
foreach (var item in Final2)
{
Console.WriteLine(item.Key + "-" + item.Value.myValue);
}
}
答案 1 :(得分:0)
根据我对您的问题的理解,您希望根据第二个词典值查找第一个词典。
public class Test
{
public string MyValue {get;set;}
}
Dictionary<string, Test> DictOne = new Dictionary<string, Test>();
Dictionary<string, string> DictTwo = new Dictionary<string, string>();
DictOne.Add("DictOneKeyOne", new Test() { MyValue = "DictOneValueOne" } );
DictTwo.Add("DictTwoKeyOne", "DictOneKeyOne");
Test ValueFromDictOne = DictOne.ContainsKey(DictTwo["DictTwoKeyOne"]) ? DictOne[DictTwo["DictTwoKeyOne"]] : null;