none
Combine GroupBy Result (IEnumerable(of IGrouping())) RRS feed

  • Question

  • I am looking for a way to combine the result of group by operation.


            Dim list = System.Diagnostics.Process.GetProcesses()
            Dim groups = list.GroupBy(Function(x) x.ProcessName)



    In the above code, I have a list of processes. I can easy group them by ProcessName. However if I only got the grouped result, how can I get back a list of processes? Certainly I can do in this way:


            Dim sameList = Enumerable.Empty(Of System.Diagnostics.Process)()
            For Each g In groups
                sameList = sameList.Concat(g)
            Next



    But I am wondering if there is any better way to do.
    • Edited by Very Crazy Dog Tuesday, June 30, 2009 7:48 AM The insert code block bad.... Hopefully the final version
    Tuesday, June 30, 2009 7:43 AM

Answers

  • I believe SelectMany() should do it.

        var list = System.Diagnostics.Process.GetProcesses();
    
        var groups = list.GroupBy(x => x.ProcessName);
    
        var sameList = groups.SelectMany(g => g);
    

    Pardon my C#, my VB.NET is still wobbly.
    Tuesday, June 30, 2009 12:19 PM

All replies

  • I believe SelectMany() should do it.

        var list = System.Diagnostics.Process.GetProcesses();
    
        var groups = list.GroupBy(x => x.ProcessName);
    
        var sameList = groups.SelectMany(g => g);
    

    Pardon my C#, my VB.NET is still wobbly.
    Tuesday, June 30, 2009 12:19 PM
  • Thanks! That works!

    The corresponding VB code:


            Dim list = System.Diagnostics.Process.GetProcesses()
            Dim groups = list.GroupBy(Function(x) x.ProcessName)
            Dim sameList = groups.SelectMany(Function(x) x)
    Wednesday, July 1, 2009 4:12 AM