c#查找List<Dictionary>中字典的Key

之前找了一些方法,尝试如何找出在List中字典的Key,发现这个方法可以实现,特此留下笔记:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Dictionary<int, int>> listDic = new List<Dictionary<int, int>>();

            Dictionary<int, int> dic = new Dictionary<int, int>();
            dic.Add(11, 22);
            dic.Add(22, 33);

            Dictionary<int, int> dic2 = new Dictionary<int, int>();
            dic2.Add(44, 55);

            listDic.Add(dic);
            listDic.Add(dic2);

            var q = listDic.SelectMany(x => x.Where(y => y.Key == 22));

            foreach (var item in q)
            {
                Console.WriteLine(item.Key + " Value is " + item.Value);
            }

            Console.ReadLine();
        }
    }
}

达到找到Key的效果:

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页