简体   繁体   中英

Conditions on foreach loop using Linq

I have a 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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