我在这里要完成的是每年返回1辆车,为期十年。 我得到的是10辆汽车,都是2013年的汽车。 这应该按2013年2012年等排序。它应该是那年特定召回次数最多的汽车。
public static IEnumerable<MakeModel> mostrecalledmodelbyYear
{
get
{
var groups = from t in DBCacheHelper.recallslist
where t.YEARTXT != "9999"
group t by new { t.YEARTXT, t.MODELTXT, t.MAKETXT } into g
select new MakeModel() { MODELTXT = g.Key.MODELTXT, MAKETXT = g.Key.MAKETXT, Frequency = g.Count(),YEARTXT = Convert.ToInt32(g.Key.YEARTXT) };
return groups.OrderByDescending(g => g.YEARTXT).Take(10);
}
}
答案 0 :(得分:0)
应该是那样的
var query=
(from t in DBCacheHelper.recallslist
where t.YEARTXT != "9999"
group t by t.YEARTXT into g
let q = g.GroupBy(t => new {t.MODELTXT, T.MAKETXT})
.Select(g2 => new {
key = g2.Key,
freq = g2.Count()
})
.OrderByDescending(t => t.freq)
let max = q.FirstOrDefault()
select new MakeModel {
MODELTXT = max.key.MODELTXT,
MAKETXT = max.key.MAKETXT,
Frequency = max.freq,
YEARTXT = g.Key//care, Convert.ToInt32 won't work in linqtoentities
})
.OrderByDescending(f => f.YEARTXT)
.Take(10);