Sorry for the possibly stupid question, but I have this Ienumerable<> set as such

I need to split out the name and percent into separate rows, and duplicate the ProductId and RowIndex for those rows (I know it's not efficient but it's what needs to be done). And probably an new field to specify which data that combined row has.
So for example,
ProductId, Name , Percent, RowIndex
2301283 , PLACEHOLDER, 12.20 , 1
should turn into this:
ProductId, DataType, Value , RowIndex
2301283 , Name , PLACEHOLDER, 1
2301283 , Percent , 12.20 , 1
etc etc
Also, they can't be nested in other lists or enumerables or anything, if that makes sense.
Is that possible within LINQ?
From what I can see, you want to transform each GI object into 2 new objects, and flatten all these pairs into one enumerable. Hopefully I understood you correctly.
I think SelectMany can do the job.
yourEnumerable.SelectMany(x => new[] {
new { ProductId = x.ProductId, DataType = "Name", Value = x.Name, RowIndex = x.RowIndex },
new { ProductId = x.ProductId, DataType = "Percent", Value = x.Percent, RowIndex = x.RowIndex }
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With