This query seems correct in terms of the result.
But in your inner query, tis.Where(p => p.PlanProgress != null && p.PlanProgress > 0).Max(w => w.EndDate) calculated for each item in the collection using t.PlanProgress > 0
Thus, this is the best way to get the Max value outside the query as follows:
var max = tis.Where(p => p.PlanProgress != null && p.PlanProgress > 0).Max(w => w.EndDate); tis.First( t => t.PlanProgress > 0 && t.EndDate == max);
Go ahead p.PlanProgress! = Null is always true, since p.PlanProgress is not Nullable. So, our code is as follows:
var max = tis.Where(p => p.PlanProgress > 0).Max(w => w.EndDate); tis.First( t => t.PlanProgress > 0 && t.EndDate == max);
Or you can change the definition of your class and make p.PlanProgress of type Nullable:
public class TaskWeekUI { public Guid TaskWeekId { get; set; } public Guid TaskId { get; set; } public Guid WeekId { get; set; } public DateTime EndDate { get; set; } public string PersianEndDate { get; set; } public double? PlanProgress { get; set; } public double ActualProgress { get; set; } } var max = tis.Where(p => p.PlanProgress.HasValue && p.PlanProgress.Value > 0).Max(w => w.EndDate); tis.First( t => t.PlanProgress.HasValue && t.PlanProgress.Value > 0 && t.EndDate == max);
Ilya
source share