109

I am looking for a way in LINQ to match the follow SQL Query.

Select max(uid) as uid, Serial_Number from Table Group BY Serial_Number

Really looking for some help on this one. The above query gets the max uid of each Serial Number because of the Group By Syntax.

7 Answers 7

102
        using (DataContext dc = new DataContext())
        {
            var q = from t in dc.TableTests
                    group t by t.SerialNumber
                        into g
                        select new
                        {
                            SerialNumber = g.Key,
                            uid = (from t2 in g select t2.uid).Max()
                        };
        }
1
  • 1
    I want to accept both as answers, but I guess I can't so I voted for both of you guys. Thanks a BUNCH!!! Commented Oct 1, 2008 at 16:33
74
var q = from s in db.Serials
        group s by s.Serial_Number into g
        select new {Serial_Number = g.Key, MaxUid = g.Max(s => s.uid) }
2
  • I want to accept both as answers, but I guess I can't so I voted for both of you guys. Thanks a BUNCH!!! Commented Oct 1, 2008 at 15:08
  • Also note that the variable for the => lambda expression in the Max function can be anything (s => s.uid, tv => tv.uid, asdf => asdf.uid). Linq will automatically recognize it as selecting over elements of type Serial.
    – Michael
    Commented Mar 17, 2011 at 17:03
43

In methods chain form:

db.Serials.GroupBy(i => i.Serial_Number).Select(g => new
    {
        Serial_Number = g.Key,
        uid = g.Max(row => row.uid)
    });
24

I've checked DamienG's answer in LinqPad. Instead of

g.Group.Max(s => s.uid)

should be

g.Max(s => s.uid)

Thank you!

1
  • 15
    This should probably a comment on DamienG's answer.
    – Cᴏʀʏ
    Commented Mar 7, 2012 at 21:40
14

The answers are OK if you only require those two fields, but for a more complex object, maybe this approach could be useful:

from x in db.Serials 
group x by x.Serial_Number into g 
orderby g.Key 
select g.OrderByDescending(z => z.uid)
.FirstOrDefault()

... this will avoid the "select new"

2
  • I like this - do you know if it is as efficient as the other answers?
    – noelicus
    Commented Aug 25, 2016 at 7:57
  • You used the query syntax here all the way up to the end, where you switched to method syntax. Did you have to? Is the a full query syntax form for your solution?
    – Seth
    Commented Dec 30, 2016 at 22:59
7

This can be done using GroupBy and SelectMany in LINQ lamda expression

var groupByMax = list.GroupBy(x=>x.item1).SelectMany(y=>y.Where(z=>z.item2 == y.Max(i=>i.item2)));
0

Building upon the above, I wanted to get the best result in each group into a list of the same type as the original list:

    var bests = from x in origRecords
            group x by x.EventDescriptionGenderView into g
            orderby g.Key
            select g.OrderByDescending(z => z.AgeGrade)
            .FirstOrDefault();

    List<MasterRecordResultClaim> records = new 
                  List<MasterRecordResultClaim>();
    foreach (var bestresult in bests)
    {
        records.Add(bestresult);
    }

EventDescriptionGenderView is a meld of several fields into a string. This picks the best AgeGrade for each event.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.