我必須根據他們的department
篩選Employee
。我可以對LINQ做同樣的事情。Lambda條件在加入聲明
Linq
和lambda
編譯得到相同的結果。編譯器在編譯之前會將查詢表達式更改爲等效的Lambda expression
,因此生成的IL完全相同。 Source
var deptCollection = new List<Dept>();
var employeeCollection = new List<Employee>();
employeeCollection.Add(new Employee { Id = 1, Name = "Eldho" });
deptCollection.Add(new Dept { DepetarmentName = "a", EmployeeId = 3 });
deptCollection.Add(new Dept { DepetarmentName = "a", EmployeeId = 1 });
var empinadept = (from e in employeeCollection
from dep in deptCollection
where e.Id == dep.EmployeeId
&& dep.DepetarmentName == "a"
select e)
.ToList();
我能不能添加
.Where
條款在此拉姆達
var empindeptLamda = employeeCollection
.Join(deptCollection,
emp => emp.Id, dep => dep.EmployeeId,
(em, dep) => em.Id == dep.EmployeeId
&& dep.DepetarmentName == "a")
.ToList();
class Employee
{
public int Id { get; set; }
public string Name { get; set; }
}
class Dept
{
public int EmployeeId { get; set; }
public string DepetarmentName { get; set; }
}
Q1。上述linq的等效lambda表達式是什麼?(如何where子句添加爲LINQ的方法,語法
使用任何格式更可讀... –
[是Linq還是Lambda?](http://stackoverflow.com/questions/7391370/is-it-linq-or-lambda)這篇文章很不錯 – Timmy
基本上都是Linq,但是一個是查詢語法,另一個是方法語法。 – HimBromBeere