C# 针对复杂数据结构的单一linq查询:Dict<;int,Dict<;str,_类>&燃气轮机;

C# 针对复杂数据结构的单一linq查询:Dict<;int,Dict<;str,_类>&燃气轮机;,c#,linq,C#,Linq,让我们先看一下下面的结构(我试图简化它,以显示我想要什么样的查询) 类名称地址 { 公共字符串名称{get;set;} 公共字符串地址{get;set;} 公共名称地址(字符串sName,字符串sAddress){Name=sName;Address=sAddress;} } 静态void Main(字符串[]参数) { 字典_Dictionary=新字典() { {/*int Key=*/1,/*字典值(1)=*/new Dictionary(){{{“First”,new Name

让我们先看一下下面的结构(我试图简化它,以显示我想要什么样的查询)

类名称地址
{    
公共字符串名称{get;set;}
公共字符串地址{get;set;}
公共名称地址(字符串sName,字符串sAddress){Name=sName;Address=sAddress;}
}
静态void Main(字符串[]参数)
{
字典_Dictionary=新字典()
{ 
{/*int Key=*/1,/*字典值(1)=*/new Dictionary(){{{“First”,new NameAddress(“John Doe”,“Mexico”)},
{/*int Key=*/2,/*字典值(2)=*/new Dictionary(){{“第二”,新名字地址(“莫里斯”,“华盛顿”)},{“第三”,新名字地址(“基思”,“纽约”)}
};    
}
我想使用单个linq查询查询以下数据结构。 比如说,我想找到住在纽约的那个人,还有他的钥匙。整数键(_dictionarykey)并不是那么重要,但是字符串键(在本例中为“Third”)是我想要找到的


是否可以在单个linq查询中找到字符串键和特定的NameAddress?如果是,该查询是什么?

这不是支持此操作的有效数据结构。如果这是一个普通的操作,你应该考虑一个新的设计。 检查文化要求

string address = "New York";
KeyValuePair<string, NameAddress> result =
    _dictionary.Values
               .SelectMany(value => value)
               .FirstOrDefault(pair => pair.Value.Address.Equals(address, StringComparison.CurrentCultureIgnoreCase));
string address=“纽约”;
KeyValuePair结果=
_字典。价值观
.SelectMany(值=>value)
.FirstOrDefault(pair=>pair.Value.Address.Equals(Address,StringComparison.CurrentCultureIgnoreCase));

请注意,此在字典上迭代(而不是使用索引),但它可以:

  var found = (from outer in _dictionary
               from inner in outer.Value
               where inner.Value.Address == "New York"
               select new {
                   OuterKey = outer.Key,
                   InnerKey = inner.Key,
                   NameAddress = inner.Value
               }).FirstOrDefault();

我同意,这是没有效率的。我试图做一些反向查找(为了好玩),结果被卡住了。因此,出于好奇,我在这里问。我只是在寻找这样的东西。不知道如何进行嵌套查询。谢谢
  var found = (from outer in _dictionary
               from inner in outer.Value
               where inner.Value.Address == "New York"
               select new {
                   OuterKey = outer.Key,
                   InnerKey = inner.Key,
                   NameAddress = inner.Value
               }).FirstOrDefault();