繁体   English   中英

使用linq订购keyvaluepair

[英]order keyvaluepair using linq

我有以下列表键值对

   var countryList = new List<KeyValuePair<string,int>>();

然后我从数据库中填充一个while循环,就像这样

   countryList.Add(new KeyValuePair<string, int>("cname",1));

然后我想使用linq查询它并同时订购它,我有这个

var lst = from s in countryList
      orderby s.[0]
      select s;

如您所知,它不起作用,我知道为什么是它。[0] 但是有人可以告诉我正确的语法是什么吗?

谢谢

var lst = from s in countryList
      orderby s.Key
      select s;

我会改为一行

var sortedCountryList = countryList.OrderBy(s=>s.Key);

如果您只想在列表中选择国家/地区名称

var sortedCountryList = countryList.OrderBy(s=>s.Key).Select(s=>s.Value);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM