我如何编写linq查询 [英] How do i write linq query
问题描述
这是我的情景:
办公室有两个分支
branch_1 , branch_2
branch_1 dept_1_1 和 dept_1_2
branch_2 dept_2_1 和 dept_2_2
每个 deprtment 有员工。
但我需要通过Office查询 dept_2_2
所有员工的名称 >
我很感谢帮我写linq查询。
我尝试了什么:
我试过这样的事情。
var query = office.Branch.First()。Dept.First()。Employee.First()。Name
我不知道Branch.First()这是branh_1。我想要branch_2。
以同样的方式我想要Dept_2_2而不是Dept_2_1。然后我想从Dept_2_2中选择所有员工并想要打印他们的名字。
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.
推荐答案
这个教程
C#中的101个LINQ示例 [ ^ ]
嗯... 第一个() [ ^ ]方法返回在指定谓词函数中通过测试的序列中的第一个元素。所以,这个查询不合适:
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:
var query = office.Branch.First().Dept.First().Employee.First().Name
尝试这样的事情:
Try something like this:
var query = office.Branch
.Where(x=>x.BranchID == "branch_2")
.Dept
.Where(x=>x.DeptID=="dept_2")
.Employee
.Select(x=>x.Name);
不要忘记按照解决方案1中的链接Beginner Luck [ ^ ]。
这篇关于我如何编写linq查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!