Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# LINQ - Split Rows into Two Rows

Tags:

c#

mysql

linq

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

Dataset

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?

like image 739
impo Avatar asked Apr 09 '26 11:04

impo


1 Answers

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 }
})
like image 132
Sweeper Avatar answered Apr 11 '26 02:04

Sweeper