Shayan
Shayan

Reputation: 402

LINQ- Max in where condition

I have a class TaskWeekUI with this definition:

 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; }    } 

and I wrote this query :

 TaskWeekUI ti =  tis.First( t => t.PlanProgress > 0 && t.EndDate ==  tis.Where(p => p.PlanProgress != null && p.PlanProgress > 0).Max(w => w.EndDate));

Is this query is true? Can I write my query better than this?

Upvotes: 13

Views: 52549

Answers (4)

Varinder Singh Baidwan
Varinder Singh Baidwan

Reputation: 203

     var max_Query =
                   (from s in db.Songs
                    join bk in db.Albums on s.BookId equals addAlbumDetailsViewModel.BookId
                    select s.SongId).Max();
                            max_Query++;

Upvotes: 0

Cheng Chen
Cheng Chen

Reputation: 43523

I think you want the one whose PlanProgress > 0 has a most recent EndDate.

TaskWeekUI ti = tis.Where(t => t.PlanProgress > 0)
                   .OrderByDescending(t => t.EndDate)
                   .FirstOrDefault();

Upvotes: 39

Eugene Gluhotorenko
Eugene Gluhotorenko

Reputation: 3164

You don't need compare PlanProgress with null because double is struct type, it can't be null.

If you want TaskWeekUI with Max EndDate and positive PlanProgress You can try this code:

TaskWeekUI ti = tis.Where(t => t.PlanProgress > 0).Max(w => w.EndDate);

Upvotes: -1

ILya
ILya

Reputation: 2778

This query seems to be correct from point of view of result obtained.

But in your inner query tis.Where(p => p.PlanProgress != null && p.PlanProgress > 0).Max(w => w.EndDate) is computed for each element in collection with t.PlanProgress > 0

So its a better way to obtain Max value outside of a 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);

Going further p.PlanProgress != null is allways true since p.PlanProgress is not of Nullable type. So our code becomes like this:

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 a definition of your class and make p.PlanProgress of Nullable type:

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);

Upvotes: 5

Related Questions