Boon Chye Phang
asked on
C# list of object concatenation and grouping
In C#, I have a list of object and want to concatenate its nested object property value by another property of the same value. These the classes and example:
How to concatenate each serialNumber into a new line of GrnLine object having the same productCode? So it will be something like:
public class Grn
{
public string referenceNumber { get; set; }
public List<GrnLine> grn_line { get; set; }
}
public class GrnLine
{
public string productCode { get; set; }
public string serialNumber { get; set; }
}
List<Grn> grnList1 = new List<Grn>() {
new Grn { referenceNumber = "ref1", grn_line = new List<GrnLine> {
new GrnLine { productCode = "productA", serialNumber="A1" },
new GrnLine { productCode = "productA", serialNumber="A2" },
new GrnLine { productCode = "productB", serialNumber="B1" }
} },
new Grn { referenceNumber = "ref2", grn_line = new List<GrnLine> {
new GrnLine { productCode = "productC", serialNumber="C1" },
new GrnLine { productCode = "productD", serialNumber="D1" },
new GrnLine { productCode = "productD", serialNumber="D2" }
} }
};
How to concatenate each serialNumber into a new line of GrnLine object having the same productCode? So it will be something like:
List<Grn> grnList2 = new List<Grn>() {
new Grn { referenceNumber = "ref1", grn_line = new List<GrnLine> {
new GrnLine { productCode = "productA", serialNumber="A1" + Environment.NewLine
+ "A2"},
new GrnLine { productCode = "productB", serialNumber="B1" }
} },
new Grn { referenceNumber = "ref2", grn_line = new List<GrnLine> {
new GrnLine { productCode = "productC", serialNumber="C1" },
new GrnLine { productCode = "productD", serialNumber="D1" + Environment.NewLine
+ "D2"},
} }
};
What was the expected output
ASKER
It will be a new List<Grn>. Just like the "grnList2"
You could do something like this:
using System;
using System.Collections.Generic;
using System.Linq;
namespace EE_Q29111505
{
class Program
{
static void Main(string[] args)
{
var data = new List<Grn>
{
new Grn
{
ReferenceNumber = "Reference1",
GrnLines = new List<GrnLine>
{
new GrnLine { ProductCode = "ProductA", SerialNumber = "A1" },
new GrnLine { ProductCode = "ProductA", SerialNumber = "A2" },
new GrnLine { ProductCode = "ProductB", SerialNumber = "A1" }
}
},
new Grn
{
ReferenceNumber = "Reference2",
GrnLines = new List<GrnLine>
{
new GrnLine { ProductCode = "ProductC", SerialNumber = "C1" },
new GrnLine { ProductCode = "ProductD", SerialNumber = "D1" },
new GrnLine { ProductCode = "ProductD", SerialNumber = "D2" }
}
}
};
var concatenated = (from grn in data
select new Grn
{
ReferenceNumber = grn.ReferenceNumber,
GrnLines = (from line in grn.GrnLines
group line by line.ProductCode into codes
select new GrnLine
{
ProductCode = codes.Key,
SerialNumber = string.Join(Environment.NewLine, (from code in codes.ToList() select code.SerialNumber))
}).ToList()
});
foreach (var concat in concatenated)
{
Console.WriteLine(concat);
Console.WriteLine();
}
Console.ReadLine();
}
}
class Grn
{
public string ReferenceNumber { get; set; }
public List<GrnLine> GrnLines { get; set; }
public override string ToString()
{
return $"{{ ReferenceNumber: {ReferenceNumber}, GrnLines: {string.Join(", ", (from line in GrnLines select $"{{ {line} }}"))} }}";
}
}
class GrnLine
{
public string ProductCode { get; set; }
public string SerialNumber { get; set; }
public override string ToString()
{
return $"{{ ProductCode: {ProductCode}, SerialNumber: {SerialNumber} }}";
}
}
}
Which produces the following output --saige-
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.