my db returns a list of object3 which i want to transform to object1 with list of items(object2) (has same same properties which are in object3)
List<object3> test()
{
var objectx = new object3
{
property1 = "x1",
property2 = "x2",
property3 = "x3",
property4 = "x4"
};
var objectxx = new object3
{
property1 = "x1",
property2 = "x2",
property3 = "x3",
property4 = "x4"
};
return new List<object3>() { objectx, objectxx };
}
}
class object1 {
public string property1 { get; set; }
public string property2 { get; set; }
public List<object2> Items { get; set; }
}
class object2
{
public string property3 { get; set; }
public string property4 { get; set; }
}
class object3
{
public string property1 { get; set; }
public string property2 { get; set; }
public string property3 { get; set; }
public string property4 { get; set; }
}
My code which doesn't work, output is 2 records of object1, but i want 1 record of object1 with 2 items.
var x = test().Select(x => new object1
{
property1 = x.property1,
property2 = x.property2,
Items = new List<object2> {
new object2
{
property3 = x.property3,
property4 = x.property4
}
}
});
object3will create only 1 item in your listpublic List<object2> items {get; set;}why do you need a list? Actually your code looks correct. But we don't know what you expect and what you actually get. You need to tells us this stuffItemsinstead ofitems(I'm assuming that's just a typo in the question). Other than that, your code works just fine andoutput1returns the same number of elements as the source list. Please try to provide a minimal reproducible example (i.e., one that can be copied and pasted) like I just did.