Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi i have the below data , employee column consider as parent and each employee have invoice as child and each invoice have invoicedetails as subchild. i have binded this data into a list of class. now i want create hierarchy list from this list using LINQ

parent--->child--->subchild
Employee--->Invoice--->InvoiceDetails

List Data:

Employee invoice InvoiceDetails jan feb Mar Apr
E1 I1 ID1 1 2 10 5
E1 I1 ID2 1 3 11 6
E1 I1 ID3 1 4 12 7
E1 I2 ID1 1 5 13 8
E1 I2 ID2 1 6 14 9
E1 I2 ID3 1 7 15 10
E2 I1 ID1 1 8 16 11
E2 I1 ID2 1 10 17 12

Output :

E1
--I1
------ID1 1 2 10 5
------ID2 1 3 11 6
------ID3 1 4 12 7
--I2
------ID1 1 5 13 8
------ID2 1 6 14 9
------ID3 1 7 15 10
E2
--I1
---------ID1 1 8 16 11
---------ID2 1 10 17 12

What I have tried:

i have tried for 2 label of class but don't have idea for 3 label of hierarchy

var query = (from f in CustomerAllocation1
                         select new Field
                         {
                             FieldId = f.FieldId,
                             //EmployeeId = e.EmployeeId,


                             //InvoiceDetailsListRows.ForEach(z => z.ChildCount = items.Count(x => x.ParentId == z.Id)),
                             Children = (from c in CustomerAllocation
                                         where c.FieldId == f.FieldId
                                         select new Customer
                                         {
                                             FieldId = f.FieldId,
                                             CustomerId = c.CustomerId,

                                             jan = c.jan,
                                             feb = c.feb,
                                             mar= c.mar
                                             april= c.april

}).tolist()
}).tolist()
Posted
Updated 17-Apr-16 20:47pm
v2
Comments
Tomas Takac 17-Apr-16 13:30pm    
This is something the ORM should do for you if configured properly. Are you using Entity framework or NHibernate or something else?

1 solution

Your code is too long. Try to shorter your code.

There is tutorial for linq 101 C#

101 LINQ Samples in C#[^]
 
Share this answer
 
Comments
san.1234 18-Apr-16 4:50am    
longer code also fine, as long as code is working i have tried for 2 level it's working , no idea ho can i do same for 3 level , could you please help me to get output for 3 level on this.

Thanks
Beginner Luck 18-Apr-16 4:52am    
show me the both lvl 2 and lvl 3 so i can see the different between them

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