Search code examples
linqgroupingprojectionlet

LINQ - Projection with grouping and Let local variable


I'm trying to group my tickets by priority and figure out the average of time to resolve each ticket within those grouped priorities. I'm not sure how to go about getting the average in projection. How do I go about inserting the formula for the average in my projection, which is essentially number tickets devide by minElapse. It says minElapse is not in the current context. Being I'm grouping and using Let this is where I get confused on how to use the variable minElapse. Thanks

var results = from r in Tickets
where r.Team.Equals("ABC") && DateCreated >1330001100 && r.DateCreated<1350000000  //unix time
let minElapse = r.FinishDate - r.DateCreated
group r by r.priority into u
select new {  
    Priority = u.Key,
TicketCount = u.Count(),
AverageMin = u.Average( c => minElapse)
};

Solution

  • You could just make it not use a let:

    var results = from r in Tickets
    where r.Team.Equals("ABC") && r.DateCreated>1330001100 && r.DateCreated<1350000000
    group r by r.priority into u
    select new 
    {  
        Priority = u.Key,
        TicketCount = u.Count(),
        AverageMin = u.Average(x => x.FinishDate - x.DateCreated)
    };