[C#] Rotate a list of generic objects.

Hello

Once upon a time I considered myself a reasonably good programmer, but apparently that's not the case – or at least this problem I am trying to solve is making me feel that way.

I have a list of objects, they are actually using generics but for the purposes of simplicity, I've hard-coded the type in the example below. I need to rotate (Transpose?) the input list. So:

var inputList = new List<InputClass>() { new InputClass() { SORT_ID = "SORT_1", NAME = "Record 1", VALUE1 = "V1", VALUE2 = "V2" }, new InputClass() { SORT_ID = "SORT_2", NAME = "Record 2", VALUE1 = "V3", VALUE2 = "V4" }, new InputClass() { SORT_ID = "SORT_3", NAME = "Record 3", VALUE1 = "V5", VALUE2 = "V6" } }; 

Needs to be converted to:

var outputList = new List<OutputClass>() { new OutputClass() { PROP1 = "SORT_1", PROP2 = "SORT_2", PROP3 = "SORT_3" }, new OutputClass() { PROP1 = "Record 1", PROP2 = "Record 2", PROP3 = "Record 3" }, new OutputClass() { PROP1 = "V1", PROP2 = "V3", PROP3 = "V5" }, new OutputClass() { PROP1 = "V2", PROP2 = "V4", PROP3 = "V6" } } 

I've experimented with all manner of approaches, including using Enumerable.Zip, a Dictionary to map properties from InputClass to OutputClass, nested forloops, cloning a List and removing items but nothing seems to work. I can't see the wood for the trees here, so if anyone can perhaps offer a solution I would be grateful.

And you're welcome to call me an idiot in the process, especially if your solution is painfully obvious.

Many thanks

by iusedtothinkicouldco via /r/csharp

Leave a Reply