Home > Back-end >  Creating Multiple List in foreach loop
Creating Multiple List in foreach loop

Time:03-23

I have a List which contains columns of data and I want to create n list depending on the number of different Client Code.

ClientCode SerialNo BarCode Remarks
ABC 123 AC123 Ok
ABC 1234 BC123 Ok
ABC 123 ABC123 Failed
ABC 1235 ABC13 Ok
ADF 321 321DFF OK
ADF 321 332DFF OK
AFG 1234 123ADF Ok
AFG 1234 123ADF Ok

And through the ForEachloop I should have n list(tables) group by from the List.

ClientCode SerialNo BarCode Remarks
ABC 123 AC123 Ok
ABC 1234 BC123 Ok
ABC 123 ABC123 Failed
ABC 1235 ABC13 Ok
ClientCode SerialNo BarCode Remarks
ADF 321 321DFF OK
ADF 321 332DFF OK
ClientCode SerialNo BarCode Remarks
AFG 1234 123ADF Ok
AFG 1234 123ADF Ok

My final goal is to export them to n differnt excel files, but for now I just wanna get the lists done first. These are the things that I have done

var ExcelGroupByClient = TotalSN.GroupBy(u => u.Data.ClientCode);
foreach (var Record in ExcelGroupByClient)
{
  //My Codes, which I have no idea of what to write.
}

I cant think of a way where I can hold n different list in the ForEach Loop

Thank you guys.

I forgot to mention that my ExcelGroupByClient is actually a Igrouping of Lists. [Final Solution]

 var ExcelGroupByClient = TotalSN.GroupBy(u => u.Data.ClientCode).ToList();
            
            foreach (var Record in ExcelGroupByClient)
            {
                var Datalist = Record.Select(a => new
                {
                    ClientCode = a.Data.ClientCode,
                    ItemCode = a.Data.ItemCode,
                    SerialNo = a.Data.SerialNo,
                    BarCode = a.Data.BarCode,
                    Errors = String.Join(" ", a.Errors)
                });
            }

CodePudding user response:

Here's a snippet showing how to traverse the grouping:

var list = new List<(string ClientCode, int Number)>
{
    (ClientCode: "ABC", Number: 123),
    (ClientCode: "ABC" , Number: 1235),
    (ClientCode: "ADF" , Number: 321 ),
    (ClientCode: "ADF" , Number: 1234 )
};

var byClient = list.GroupBy( x => x.ClientCode);

foreach( var group in byClient)
{
    Console.WriteLine($"Group's Key: {group.Key}");
    foreach(var record in group)
    {
        Console.WriteLine($"  ClientCode: {record.ClientCode} Number: {record.Number}");
    }
}

This prints

Group's Key: ABC
  ClientCode: ABC Number: 123
  ClientCode: ABC Number: 1235
Group's Key: ADF
  ClientCode: ADF Number: 321
  ClientCode: ADF Number: 1234

CodePudding user response:

You posted your solution into the question, you should post an answer instead. Just wanted to point out you can merge your foreach into the GroupBy because GroupBy has an overload that takes a lambda that is applied to the output:

    var groups = TotalSN.GroupBy(
        u => u.Data.ClientCode,  
        rs => rs.Select(a => new
            {
                ClientCode = a.Data.ClientCode,
                ItemCode = a.Data.ItemCode,
                SerialNo = a.Data.SerialNo,
                BarCode = a.Data.BarCode,
                Errors = String.Join(" ", a.Errors)
            }
    );

Whatever you were going to use datalist for is now applicable directly to the IGrouping

foreach(var datalist in groups){
  foreach(var thing in datalist)
    ...
}

You'll confuse yourself less if you strive to use plural or collection-flavored names for things that are collections and singular names for those that are not. Your Record is actually a collection still, so Record.Select reads a bit weird because Record is a singular but Select operates on collections.

Local variables should have camelCase names -> record

  • Related