entity framework - Linq to Get distinct List of rows in descending order -


consider following record

  id            f1            f2     f3         date   -------------------------------------------------    1           1800          1990     19      2016-06-27 09:24:25.550    2           1181          1991     19      2016-06-27 09:25:15.243    3           1919          2000     19      2016-06-27 11:04:27.807    4           1920          2000     19      2016-06-27 13:04:27.807    5           1800          2001     19      2016-06-28 09:24:25.550    6           1181          2002     19      2016-06-28 09:25:15.243     7           1919          2010     19      2016-06-28 11:04:27.807 

i want groupby f1 sorted date descending desirder output

      id            f1           f2    f3         date       -------------------------------------------------       7           1919          2010     19      2016-06-28 11:04:27.807           6           1181          2002     19      2016-06-28 09:25:15.243               5           1800          2001     19      2016-06-28 09:24:25.550       4           1920          2000     19      2016-06-27 13:04:27.807 

i have tried

    datetime enddate=datetime.now.adddays(-1);      var result = (from opt in db.output                                      opt.f3==19 && opt.date > enddate                                      orderby opt.date descending                                      select new                                       {                                          id= opt.id,                                          f1=opt.f1,                                          f2=opt.f2,                                          f3=opt.f3,                                          date=opt.date                                      }).groupby(x => x.f1).select(s => s.orderby(o => o.f2).firstordefault()).orderbydescending(x => x.date).tolist(); 

im getting output

 id            f1            f2     f3         date   -------------------------------------------------    1           1800          1990     19      2016-06-27 09:24:25.550    2           1181          1991     19      2016-06-27 09:25:15.243    3           1919          2000     19      2016-06-27 11:04:27.807    4           1920          2000     19      2016-06-27 13:04:27.807 

what wrong code.

if understand correctly want recent item of each group:

db.output.groupby(opt => opt.f1).           select(group => group.orderbydescending(opt => opt.date).first()).           orderby(opt => opt.id); 

i'm not sure translation sql efficient though due inner ordering.

now since groupby preserves order, might fix issue with:

db.output.orderbydescending(opt => opt.date).           groupby(opt => opt.f1).           select(group => group.first().           orderby(opt => opt.id); 

Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -