Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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



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

What I have tried:

I tried something like this.

var query = office.Branch.First().Dept.First().Employee.First().Name

I don't what Branch.First() which is branh_1. I want branch_2.
In the same way I want Dept_2_2 not Dept_2_1. and then i want to select all employee from Dept_2_2 and want to print their names.
Posted
Updated 21-Feb-16 21:55pm
v3
Comments
[no name] 21-Feb-16 23:56pm    
Provide table structure for above requirement?
s23user 22-Feb-16 0:16am    
I am trying to do linq to object (in Memory) by creating classes and properties and object initialization. Basically I do have classes for Office, branch , departments and employees. Office has collection of branch. Branch has collection of department and department has list of employees.
s23user 22-Feb-16 1:51am    
I don't know how but I guess it is something to do with projection. I hope u know it better.
Maciej Los 22-Feb-16 3:09am    
"What have you tried" section is to provide information about your attempts, but you wrote a request for code only.
s23user 22-Feb-16 3:37am    
I updated the question.

This the tutorial

101 LINQ Samples in C#[^]
 
Share this answer
 
Comments
Maciej Los 22-Feb-16 3:09am    
Sure, a 5!
Beginner Luck 22-Feb-16 3:17am    
Thank you
Well... First()[^] method returns the first element in the sequence that passes the test in the specified predicate function. So, this query is not proper:
C#
var query = office.Branch.First().Dept.First().Employee.First().Name


Try something like this:
C#
var query = office.Branch
                  .Where(x=>x.BranchID == "branch_2")
                  .Dept
                  .Where(x=>x.DeptID=="dept_2")
                  .Employee
                  .Select(x=>x.Name);


And do not forget to follow the link in solution 1 by Beginner Luck[^].
 
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