I have a list of items. I want to loop through the list and merge all members that have the same CN8, MSConsDestCode and countryOfOriginCode.
Also netMass should be a subtotal of netMass properties of merged items.
public class Item
{
public int CN8 { get; internal set; }
public string goodsDescription { get; internal set; }
public string MSConsDestCode { get; internal set; }
public string countryOfOriginCode { get; internal set; }
public decimal netMass { get; internal set; }
}
From this:
var data = new List<Item>
{
new Item {CN8 = 123456789, goodsDescription = "blah0", MSConsDestCode = "DE", countryOfOriginCode = "CN", netMass = 1 },
new Item {CN8 = 123456789, goodsDescription = "blah1", MSConsDestCode = "DE", countryOfOriginCode = "CN", netMass = 1 }
new Item {CN8 = 123456789, goodsDescription = "blah2", MSConsDestCode = "GB", countryOfOriginCode = "IN", netMass = 1 }
},
I'm looking for a result like this: (first two items merged, since they have the same CN8, MSConsDestCode and countryofOriginCode, last item is the same as in previous list)/
{CN8 = 123456789, goodsDescription = "blah0", MSConsDestCode = "DE", countryOfOriginCode = "CN", netMass = 2 }
{CN8 = 123456789, goodsDescription = "blah2", MSConsDestCode = "GB", countryOfOriginCode = "IN", netMass = 1 }
Note: goodsDescription can be random name from any element that was merged.
I have a feeling I should try to use LINQ for this, but since I'm not really familiar with it, I'm asking you guys to guide me to a right direction.