0

我在这里试图完成的是十年内每年归还一辆汽车。我得到的是 10 辆都是 2013 年的汽车。这应该按 2013 年 2012 年 2011 年等排序。它应该是该给定年份召回次数最多的汽车。

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

1 回答 1

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);
于 2012-09-26T06:52:04.967 回答