-1

I am new to Linq and I just started to study Linq query. I believe this is something to do with projection. But I am clueless how to dig into it to get what I desired in my result. This is my Scenario : Office which has two branches

branch_1, branch_2

branch_1 has dept_1_1 and dept_1_2

branch_2 has dept_2_1 and dept_2_2

each deprtment has employees.

But I need to query through Office to find name of all the employees in dept_2_2 and dept_2_2 is in branch_2

This is something I did.

var result = office.Branch.First()

But first() will give me branch_1 which I do not want how do I get branch_2 and then further dept_2_2 and then all the employees .

I would be thankful to help me out to write linq queries.

slawekwin
  • 6,270
  • 1
  • 44
  • 57
Nepsydaz
  • 27
  • 1
  • 9
  • 2
    Would you please provide us your code and if possible some sample data so we can see where the problem is? – Marco Feb 22 '16 at 08:23
  • look at this : http://stackoverflow.com/questions/20974248/recursive-hierarchy-recursive-query-using-linq – Jurion Feb 22 '16 at 08:23

2 Answers2

0
  var result = from branch in office.Branches
               where branch.Name == "branch2"
               from dept in branch.Departments
               where dept.Name == "dept2"
               select dept.Employees.ToList();

This gives IEnumerable list of all the employees in the branch2 -> dept2

hari6
  • 14
  • 2
  • I have just started to learn Linq to object. So I am trying to query in in memory collection of object. I have an Office class which has List, Branch has List and dept has List and Employee has name property. – Nepsydaz Feb 22 '16 at 08:53
  • @gaurav updated my answer according to your comment. – hari6 Feb 23 '16 at 02:56
0

I think type of office List<Branch> . Please try this. I hope this helps to you:

        List<Employee>listOfEmployee = office.Where(l =>l.Branch == branch_2 
                                                          &&l.Branch.DeptName == dept_2_2 )
                                                          .Select(l => l.Branch.Dept.Employee.EmpName)
                                                          .ToList();