Linq differences

I know this isn’t stack overflow but I figured since we are in the same world of LINQ, SQL and DBs I would pose this question.

What are the fundamental differences between the following bits of code? Do they both have deferred datasource access?

 var numQuery =
                numbers.Where(num => num % 2 == 0);

vs

 var numQuery =
            from num in numbers
            where (num % 2) == 0
            select num;

@josecgomez come on, be my Jon Skeet

There is no practical difference. One is just a lambda implementation

Jos? C Gomez
Senior Software Engineer

1 Like

Jos? C Gomez
Senior Software Engineer

@Chris_Conn yes, they are both deferred execution. For immediate execution a query would be something like below or the use of an immediate operator (count, sum, etc.). The deferred execution is the better way to go for performance.

var emp = Employees.Where(x => x.Salary < 35000)
	  	   .Select(y => y.Name).ToList();
2 Likes