简体   繁体   English

使用Linq在foreach循环上的条件

[英]Conditions on foreach loop using Linq

I have a List<KeyValuePair<string, Records> mylist; 我有一个List<KeyValuePair<string, Records> mylist;

I would like to loop in the list foreach different key value. 我想在列表中循环查看每个不同的键值。 Something like 就像是

foreach(var item in myList.select(/\*query the disctinct values of key here ?*/)

is it possible ? 可能吗 ? How ? 怎么样 ?

You can do 你可以做

foreach(var item in myList.Select(x => x.Key).Distinct())
{
    //Your Logic
}

myList.Select(x => x.Key).Distinct() would give you distinct Keys and foreach will loop over them. myList.Select(x => x.Key).Distinct()将为您提供不同的键,并且foreach将遍历它们。

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

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