使用Lambda表达式从c#字典过滤和检索数据

时间:2019-06-10 05:13:03

标签: c# .net linq lambda

我有ListA,而ListAListB 我需要检索ListA的所有 key ,其中在ListB

中有特定的 Key

例如:

如果我将123作为 listB key ,我应该得到<ABC,UVW>

如果我将246作为ListB Key 给出,我应该得到<ABC>

ListA [0]

Key: ABC (double)
Value:
Desc: XYZ (string)
ListB[0]
    Key:123
    Val:321
ListB[1]
    Key: 246
    Val: 642

ListA [1]

Key: UVW
Value:
Desc: XYZ
ListB[0]
    Key:123 (string)
    Val:321 (double)
ListB[1]
    Key: 343
    Val: 100

选择ListA的所有键,其中ListB.Key = 123

表达尝试过

ListA.Values.Where(b => b.ListB.Key==123).Select(a=>a.Key)
  

错误:错误CS1061:“词典”不包含   “键”的定义,并且没有可访问的扩展方法“键”   接受类型为“字典”的第一个参数可以   被找到(您是否缺少using指令或程序集引用?)

ListA.Where(p => p.ListB.Contains(123)).Select(p=> p.ListA.ToList()
  

错误CS1061:“ KeyValuePair”不包含   “ ListB”的定义,没有可访问的扩展方法   可以找到接受类型为'KeyValuePair的第一个参数的ListB(您是否缺少using指令或   程序集参考?)

在ids下面是defn类

class cListA

{
private Dictionary<string, double> ListB = new Dictionary<string, double>();

private string Desc;

public Dictionary<string, double> ListBvar
    {
        get
        {
            return ListB
        }
    }


    public string sDesc
    {
        get
        {
            return Desc
        }
    }
}

private Dictionary <string, cListA> ListA= new Dictionary <string, cListA>(); 

1 个答案:

答案 0 :(得分:0)

我明白了,答案很简单。

ListA.Values.Where(p=> 
       p.ListB.Values.Contains(123)).Select(a=>a.Key).ToList().
相关问题