Get relational data into class hirarchy [Resolved]

Posted by Santosh4u under LINQ on 2/20/2016 | Points: 10 | Views : 452 | Status : [Member] | Replies : 3
Hi,
i need to bind relational data from sqlserver table into class hierarchy.
providing below example for more clarification.

tbl_Company
Company_ID Com_Name
1 Com1
2 Com2

tbl_Employee
Employee_Id Emp_Name Company_Id
1 Emp1 1
2 Emp2 2

tbl_Invoice
Invoice_Id Invoice_Name Employee_Id
1 Invoice1 1
2 Invoice2 1
3 Invoice3 3

i have created classes for Company,Employee,Invoice
and able to bind dynamically just adding value into class using loop
for (int i = 0; i < 10; i++)
{
Company company = new Company("CN " + i.ToString());
Employee emp1 = new Employee("Employee1 " + i.ToString(), i = i++);
Employee emp2 = new Employee("Employee2 " + i.ToString(), i = i++);
Invoice empCou = new Invoice("INV " + i.ToString(), i = i++);
Invoice empCou1 = new Invoice("INV2 " + i.ToString(), i = i++);
emp1.EmployeeCou.Add(empCou);
emp1.EmployeeCou.Add(empCou1);
emp2.EmployeeCou.Add(empCou1);
company.Employees.Add(emp1);
companies.Add(company);
}

how can i achieve the above scenario using LINQ.(company class should contain hierarchical data based on foreign key relation)


Thanks




Responses

Posted by: Rajnilari2015 on: 2/20/2016 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
Try this (using statement lambda),
Enumerable
.Range(0,9)
.ToList()
.Foreach(
i=>
{
Employee emp1 = new Employee(String.Concat("Employee1 ",i), i = i++);
Employee emp2 = new Employee(String.Concat("Employee2 ",i), i = i++);
emp1.EmployeeCou.Add(new Invoice(String.Concat("INV",i), i = i++)));
emp1.EmployeeCou.Add(new Invoice(String.Concat("INV2",i), i = i++));
emp2.EmployeeCou.Add(new Invoice(String.Concat("INV2",i), i = i++));
company.Employees.Add(emp1);
companies.Add(new Company(String.Concat("CN",i)));
};
);


Hope this helps

--
Thanks & Regards,
RNA Team

Santosh4u, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Santosh4u on: 2/21/2016 [Member] Bronze | Points: 25

Up
0
Down
Thanks for your reply..
but i want to get all the records from database table (not hardcoded). i have provided above example with hardcoded value for reference..

hope you understand my requirement

Thanks
Santosh.



Santosh4u, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 2/21/2016 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
@Santosh4u,
There is nothing hardcoded if you take a careful observation (: .
e.g.

Enumerable .Range(0,9)

the upper range can be replaced with the total number of record counts.

Employee emp1 = new Employee(String.Concat("Employee1 ",i), i = i++);
can be replaced with Employee emp1 = new Employee(dr["EmployeeName"].ToString()); where dr is data row.

I hope you got the logic and can proceed further.

Kindly let us know if you need any further assistance.

Thanks

--
Thanks & Regards,
RNA Team

Santosh4u, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response