Doro
Doro

Reputation: 671

C# query List<Tuple> using LINQ

I have a list and I need to start reading when item1 is great than myVariable. I can do it using a loop and an if statement but can someone help me to do it using LINQ?

var listDate = new List<Tuple<DateTime, double>>();

foreach (var item in listDate)
{
    if (item.Item1 > myVariable)
         Console.WriteLine(item);
}   

Upvotes: 0

Views: 8556

Answers (2)

Konrad Kokosa
Konrad Kokosa

Reputation: 16878

An alternative solution to Where:

var result = listData.SkipWhile(t => t.Item1 <= myVariable);

Note: This assumes that collection is ordered with respect to Item1. And then "skip" may be more clear as it is closer to the intent "I need to start reading when item1 is great than myVariable" than standard collection filtering with Where.

Upvotes: 2

Tim Schmelter
Tim Schmelter

Reputation: 460098

The LINQ counterpart of if is Where:

IEnumerable<Tuple<DateTime, double>> query = listDate.Where(t => t.Item1 > myVariable);

You can use a foreach-loop or another method like ToList to consume the query.

Upvotes: 4

Related Questions