Q:

Only first “grouped row” filled with key values linq

I have a result as follow : 

I want to group by col1,col2 and col3 Columns ..but key values only show on first row :

Is that possible by c# linq??
And if yes, how?

A:

For Linq to Objects, you can do this like the bellow proposition:
1 - Group by columns (1,2 and 3)
2 - ToDictionary to convert grouped result to Dictionary by incrementing an index, will be zero for each first element in grouped result.
3 - Use SelectMany to fetch new list like the expected result.
// create class and initialize list of object for test
public class Test
{
    public string Col1 { get; set; }
    public int? Col2 { get; set; }
    public string Col3 { get; set; }
    public int Col4 { get; set; }
    public int Col5 { get; set; }
}

List<Test> tests = new List<Test>
{
    new Test { Col1 = "A", Col2 = 1 , Col3 = "a", Col4 = 10, Col5 = 20},
    new Test { Col1 = "A", Col2 = 1 , Col3 = "a", Col4 = 15, Col5 = 22},
    new Test { Col1 = "A", Col2 = 1 , Col3 = "a", Col4 = 30, Col5 = 12},
    new Test { Col1 = "B", Col2 = 2 , Col3 = "b", Col4 = 25, Col5 = 22},
    new Test { Col1 = "B", Col2 = 2 , Col3 = "b", Col4 = 10, Col5 = 15},
};

Query Linq
With .ToDictionary()
var result = tests.GroupBy(x => new { x.Col1, x.Col2, x.Col3 })
    .ToDictionary(x => x.Key, y => y.Select((z, i) => new { z, i }))
    .SelectMany(y => y.Value
        .Select(x => x.i == 0
            ? x.z :
            new Test { Col4 = x.z.Col4, Col5 = x.z.Col5 }))
    .ToList();

Without .ToDictionary()
var result = tests.GroupBy(x => new { x.Col1, x.Col2, x.Col3 }, 
    (y, w) => w.Select((z, i) => new { z, i }))
    .SelectMany(y => y
        .Select(x => x.i == 0
            ? x.z :
            new Test { Col4 = x.z.Col4, Col5 = x.z.Col5 }))
    .ToList();

Demo
foreach(Test test in result)
{
    Console.WriteLine($"Col1 : {test.Col1}, Col2 : {test.Col2}, " +
        $"Col3 : {test.Col3}, Col4 : {test.Col4}, Col5 : {test.Col5}");
}

Result
Col1 : A, Col2 : 1, Col3 : a, Col4 : 10, Col5 : 20
Col1 :  , Col2 :  , Col3 :  , Col4 : 15, Col5 : 22
Col1 :  , Col2 :  , Col3 :  , Col4 : 30, Col5 : 12
Col1 : B, Col2 : 2, Col3 : b, Col4 : 25, Col5 : 22
Col1 :  , Col2 :  , Col3 :  , Col4 : 10, Col5 : 15

I hope you find this helpful.

