views:

3415

answers:

3

Hey all,

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.

+10  A: 
        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()
                        };
        }
tvanfosson
I want to accept both as answers, but I guess I can't so I voted for both of you guys. Thanks a BUNCH!!!
Scott
+6  A: 
var q = from s in db.Serials
        group s by s.Serial_Number into g
        select new {Serial_Number = g.Key, MaxUid = g.Group.Max(s => s.uid) }
DamienG
I want to accept both as answers, but I guess I can't so I voted for both of you guys.Thanks a BUNCH!!!
Scott
+2  A: 

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!

denis_n