LINQ - 使用分组和Let局部变量进行投影

时间:2013-07-22 21:33:46

标签: linq grouping projection let

我正在尝试按优先级对票证进行分组,并计算出在这些分组优先级内解析每张票证的平均时间。我不确定如何在预测中获得平均值。如何在我的投影中插入平均值的公式,这实际上是minElapse的数字票据。它说minElapse不在当前的上下文中。因为我正在分组和使用让我对如何使用变量minElapse感到困惑。感谢

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

1 个答案:

答案 0 :(得分:1)

你可以让它不使用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)
};