Those are my classes:
List<MyDate> Dates
public class MyDate
{
public DateTime FromDate { get; set; }
public List<Tuple<DateTime, List<MyEnum>>> ToDates { get; set; }
}
I'm trying to retrieve the nearest FromDate
property to a originDate
, but I need it to do the comparison only for items that answer the statement of Where(d => d.ToDates.Any(t => t.Item2.Contains(MyEnum.Value1)))
long nearestDiff = myList
.Where(d => d.ToDates.Any(t => t.Item2.Contains(MyEnum.Value1)))
.Select(d => d.FromDate)
.Min(date => Math.Abs((date - originDate).Ticks));
The problem is when none of the items is answering the Where
statement the Select
statement throw the exception of:
System.InvalidOperationException: 'Sequence contains no elements'
I tried using the SingleOrDefault
as this answer suggested, but I need the Min value will be calculated against all values not only a single one.