tags:

views:

181

answers:

1

I have a list of rows like this (NULLs since i'm using DefaultIfEmpty)

Vol           Component         Dwg      View    Revision   

Volume U01  Composed Drawings SJTest 1 B
Volume U01  Composed Drawings SJTest 1 A
Volume U01  Composed Drawings SJTest 1 NULL
Volume U03  Composed Drawings SJTest2 2 NULL

I want to sort by the last coln, (A,B,NULL) for each group and then get only the first row. So in this case I should have the final set as something like:

Volume U01  Composed Drawings SJTest 1 B
Volume U03  Composed Drawings SJTest2 2 NULL

Any thoughts ? I'm lost on how to get this from the grouping - first row of each group.

This is LINQ-SQL...sorry for forgetting that.

thanks Sunit

A: 

Because you say that the first row of the first group should be revision "B", I'm guessing that you want to sort in descending order, so something like this?

var query = yourData
            .GroupBy(x => new { x.Vol, x.Component, x.Dwg, x.View })
            .Select(g => g.OrderByDescending(x => x.Revision).First());

// and to test...
foreach (var x in query)
{
    Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}",
        x.Vol, x.Component, x.Dwg, x.View, x.Revision ?? "NULL");
}

// Volume U01    Composed Drawings    SJTest     1    B
// Volume U03    Composed Drawings    SJTest2    2    NULL
LukeH
This worked with a small edit:.GroupBy(x=>new {x.Volumne, x.Component, x.Drawing}, x=>x).Select(g=>g.First());
Sunit