I have a flat object, and it looks like as below
public class Test
{
public string Name {get; set;}
public string Number {get; set;}
public string Description {get; set;}
public string Name1 {get; set;}
public string Name2 {get; set;}
}
and I have the data looks like as below
List<Test> tests = new List<Test>
{
new Test{ Name = "nameA", Number=8, Description="description", Name1 = "test 1", Name2="test name1" },
new Test{ Name = "nameB", Number=3, Description="description2", Name1 = "test 2", Name2="test name2" },
new Test{ Name = "nameC", Number=4, Description="description3", Name1 = "test 3", Name2="test name3" },
};
and I am trying to form an object list that looks like below
I am using the below logic to form a list object but failed to add all objects to that,
List<dynamic> testList = new();
int rowIndex = 1;
foreach (var testDt in tests)
{
dynamic testData = new ExpandoObject();
if (rowIndex % 2 != 0)
{
testData.Name = testDt.Name;
testData.Description = testDt.Description;
testData.Number = testDt.Number;
}
else
{
testData.Name1 = testDt.Name1;
testData.Name2 = testDt.Name2;
}
testList.Add(testData);
rowIndex ;
}
But this logic is not adding all items to the list if the list has more items and could anyone please let me know where I am wrong with the above code.
Many thanks in advance!!!
CodePudding user response:
Here you go:
List<Test> tests = new List<Test>
{
new Test{ Name = "nameA", Number = 8, Description = "description", Name1 = "test 1", Name2 = "test name1" },
new Test{ Name = "nameB", Number = 3, Description = "description2", Name1 = "test 2", Name2 = "test name2" },
new Test{ Name = "nameC", Number = 4, Description = "description3", Name1 = "test 3", Name2 = "test name3" },
};
var results =
from t in tests
from r in new []
{
new { t.Name, Number = t.Number.ToString(), t.Description, Name1 = "", Name2 = "", },
new { Name = "", Number = "", Description = "", Name1 = t.Name1, t.Name2, },
}
select r;
That gives me:
To use a regular foreach
loop this would work:
public class Output
{
public string Name { get; set; }
public string Number { get; set; }
public string Description { get; set; }
public string Name1 { get; set; }
public string Name2 { get; set; }
}
var results = new List<Output>();
foreach (var t in tests)
{
results.Add(new Output() { Name = t.Name, Number = t.Number.ToString(), Description = t.Description, Name1 = "", Name2 = "", });
results.Add(new Output() { Name = "", Number = "", Description = "", Name1 = t.Name1, Name2 = t.Name2, });
}
I've created a new class rather than use dynamic
, as dynamic
generally creates more problems than it solves, IMO. I avoid it where possible.
And finally, the equivalent using LINQ's method syntax:
results =
tests
.SelectMany(t => new []
{
new Output() { Name = t.Name, Number = t.Number.ToString(), Description = t.Description, Name1 = "", Name2 = "", },
new Output() { Name = "", Number = "", Description = "", Name1 = t.Name1, Name2 = t.Name2, },
})
.ToList();
CodePudding user response:
The problem is that you are adding only 1 item to the list for each iterate, however, what you need is to add 2 instead to be able to get the expected result.
You can simply do that as follows.
foreach (var testDt in tests)
{
dynamic testData1 = new ExpandoObject();
testData1.Name = testDt.Name;
testData1.Description = testDt.Description;
testData1.Number = testDt.Number;
testList.Add(testData1);
dynamic testData2 = new ExpandoObject();
testData2.Name1 = testDt.Name1;
testData2.Name2 = testDt.Name2;
testList.Add(testData2);
}