Ythio Csi
Ythio Csi

Reputation: 379

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 ?

Upvotes: 1

Views: 1275

Answers (1)

Nikhil Agrawal
Nikhil Agrawal

Reputation: 48568

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.

Upvotes: 4

Related Questions