Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need to flatt list<object> with sub object into 1 list.

i have
List<object1> into this list i have sub list
with properties
class object1
{
public int id;
public int name;
public list<object2> items;
}
class object 2
{
public int id;
public string name;
public decimal price;
}

for example
object 1 have id = 1, name=One with list of 3 items..

i need this in 1 flat list
List<object 3=""> flatt
object 3 will have

class object3
{
public int id;
public int name;
public int itemid;
public string name;
public decimal price;
}

i need this with linq solution. TNX

What I have tried:

class object1
{
    public int id;
    public int name;
    public list<object2> items;
}

class object 2
{
    public int id;
    public string name;
    public decimal price;
}

class object3
{
    public int id;
    public int name;
    public int itemid;
    public string name;
    public decimal price;
}


.....
Posted
Updated 30-Jul-19 4:27am
Comments
OriginalGriff 29-Jul-19 6:54am    
And?
What have you tried?
Where are you stuck?
What help do you need?
BillWoodruff 29-Jul-19 7:45am    
The fact the code you show would never compile indicates you have made no effort. Hint: study the Linq 'Select statement.

If you show edited code that actually compiles, I'll assist.

C#
var object3Query = object1List
   .Select( o1 => o1.items
      .Select( o2 => new object3(){ 
         id = o1.id, 
         name = o1.name, 
         ...,
         price = o2.price }
));
 
Share this answer
 
Comments
Maciej Los 29-Jul-19 13:55pm    
5ed!
I modified your int name field in object1 and object3 to nameid as there will be name collision.

class object1
    {
        public int id;
        public int nameId;
        public List<object2> items;
    }

class object2
{
public int id;
public string name;
public decimal price;
}

class object3
{
    public int id;
    public int nameId;
    public int itemid;
    public string name;
    public decimal price;
}


Some sample data here:

var object2List1 = new List<object2>
            {
                new object2 {id = 1, name = "1", price = 10},
                new object2 {id = 2, name = "2", price = 20},
                new object2 {id = 3, name = "3", price = 30},
            };

            var object2List2 = new List<object2>
            {
                new object2 {id = 11, name = "11", price = 11},
                new object2 {id = 21, name = "21", price = 21},
                new object2 {id = 31, name = "31", price = 31},
            };

            var object1List = new List<object1>
            {
                new object1 {id = 100, nameId = 100, items = object2List1},
                new object1 {id = 110, nameId = 110, items = object2List2},
            };


Final code to build the list of object3 is shown here. Please note the use of SelectMany in the query above.

<pre>
var object3List = object1List
                .SelectMany(o1 =>
                    o1.items.Select(o2 => new object3()
                    {
                        id = o1.id,
                        nameId = o1.nameId,
                        itemid = o2.id,
                        name = o2.name,
                        price = o2.price

                    })).ToList();



foreach (var ol in object3List)
{
   Console.WriteLine(ol.id + "\t" + ol.nameId + "\t" + ol.itemid + "\t" + ol.name + "\t" + ol.price)
};
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900