4

I have to filter Employee based on their department. I'm able to do the same with LINQ.

Linq and lambda compile to get same result. The compiler changes the query expression into the equivalent Lambda expression before compiling it, so the generated IL is exactly the same.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(); 

I can't able to add .Where Clause in this lambda

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. What is the equivalent lambda statement for the above linq ? (How to add where clause as in linq in method-syntax

7
  • 3
    Use whichever format is more readable... Commented Aug 2, 2016 at 12:35
  • 4
    Is it Linq or Lambda? This post is pretty good Commented Aug 2, 2016 at 12:37
  • 1
    Basically both are Linq, however the one is a query-syntax and the other is the method-syntax. Commented Aug 2, 2016 at 12:38
  • @AustinFrench how should i use where condition with join Commented Aug 2, 2016 at 12:39
  • You add it after the .Join or do a join with multiple conditions Commented Aug 2, 2016 at 12:41

2 Answers 2

10

The equivalent for this:

var empinadept = (from e in employeeCollection from dep in deptCollection where e.Id == dep.EmployeeId && dep.DepetarmentName == "a" select e) .ToList(); 

Is this:

var result = employeeCollection.Join(deptCollection, e => e.Id, dep => dep.EmployeeId, (e,dep) => new { e, dep }) .Where(item => item.dep.DepetarmentName == "a") .Select(item => item.e) .ToList(); 

A better option will be to:

var result = employeeCollection.Join( deptCollection.Where(dep => dep.DepetarmentName == "a"), e => e.Id, dep => dep.EmployeeId, (e,dep) => e) .ToList(); 

Closest to the query-syntax (but I would say that is less nice opinion based) is:

var result = employeeCollection.Join( deptCollection, e => new { e.Id, "a" }, dep => new { dep.EmployeeId, dep.DepartmentName }, (e,dep) => e).ToList(); 
Sign up to request clarification or add additional context in comments.

7 Comments

This works for me, when i benchmarked it, Linq is faster than lamda. The figures are 001940 ms for linq and 0058248 for lamda.
@Eldho - I wouldn't rush to say it is faster (might be true but I don't know enough) - I'd check the sql that is being generated and check. Also compare for different Databases
This are in memory objects, i have this in a List
ah ok :) also compare what happens when you play with first the Where and then the join and so on, and ToList too, but again could be that you are correct
I think using objects like HashSets will be more beneficial for the performance that if it is query or method syntaxs. I'd go with what is more readable - sometimes it is methods, sometimes it is the query - and this is already personal preference too
|
0

Q1. What is the equivalent lamda statement for the above linq ?

var empindeptLamda = employeeCollection .Join(deptCollection, emp => emp.Id, dep => dep.EmployeeId, (e, dep) => new { e, dep }) .Where(x => x.dep.DepetarmentName == "a") .Select(x => x.e) .ToList(); 

Q2. When should i choose LINQ vs Lamda method syntax or query syntax ?

This is really your personal preference. Since they are compiled into the same IL, so the performance is the same. However, there is some situation where query syntax is preferred, such as having multiple join clause. And other time where the method syntax shines, like adding your own extensions method, or debugging the intermediate result between each method.

2 Comments

you don't need the Id == EmployeeId in the Where - you already did it in the Join
@GiladGreen Yea, you are right, it was implied.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.