c#查找List中字典的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的效果:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章