The Software that Never Crashes

A LINQ lesson

2. October 2008 12:18 by Scott in   //  Tags:   //   Comments (1)

I needed to get a query completed like this in LINQ.

Select max(uid) as uid, Serial_Number from Table Group BY Serial_Number
I thought about it for a while but didn't get anywhere.  I kept testing until I asked another person and they came up with:
 
        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()
                        };
        }
 
This code is very nice and sweet.  It allows you to select the all MAX Rows of a table with a grouped table column. 
Hope you enjoyed this little lesson.  I decided to save this syntact because it is something I will most likely come accross again.
Scott
If you liked this post, please be sure to subscribe to my RSS Feed.

Comments (1) -

jerry
jerry
11/14/2011 3:51:26 AM #

Peculiar article, just what I needed.

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading