Reputation: 671
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
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
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