are these two linq the same

T

Tony Johansson

Hello!

I have two linq queries below and I wonder are these the same and will give
the same result ?
My second question. As I have understood the first one is using Lambda
expression and in the
second is just a strict Linq query. Is this correct understood ?

Is it any difference in performance between these two ?

A question about understanding for the first one.
Here you return true for those that match the string literal "Rio de
Janeiro"
but then what data is this *.Select(c => c);* operate on ?
Is this *.Select(c => c);* operate on those data that is return as true
from
Where(c => c.City == "Rio de Janeiro"



var custs = db.Customers
..Where(c => c.City == "Rio de Janeiro"
..Select(c => c);

var custs =
from c in db.Customers
where c.City == "Rio de Janeiro"
select c;

//Tony
 
M

Mr. Arnold

Tony Johansson said:
Hello!

I have two linq queries below and I wonder are these the same and will
give the same result ?
My second question. As I have understood the first one is using Lambda
expression and in the
second is just a strict Linq query. Is this correct understood ?

Is it any difference in performance between these two ?

A question about understanding for the first one.
Here you return true for those that match the string literal "Rio de
Janeiro"
but then what data is this *.Select(c => c);* operate on ?
Is this *.Select(c => c);* operate on those data that is return as true
from
Where(c => c.City == "Rio de Janeiro"



var custs = db.Customers
.Where(c => c.City == "Rio de Janeiro"
.Select(c => c);

var custs =
from c in db.Customers
where c.City == "Rio de Janeiro"
select c;

If you're using Linq-to-Sql then use this tool in debug mode and go look at
the results of the underlying SQL Select statment.

http://weblogs.asp.net/scottgu/archive/2007/07/31/linq-to-sql-debug-visualizer.aspx
 
F

Frans Bouma [C# MVP]

Tony said:
Hello!

I have two linq queries below and I wonder are these the same and will give
the same result ?

yes (well, the second will not have the .Select(c=>c) but that's
ignored in the SQL anyway.)
My second question. As I have understood the first one is using Lambda
expression and in the
second is just a strict Linq query. Is this correct understood ?

I don't know what 'strict linq query' means, but if it may help: the C#
compiler compiles the second query to code which produces the first at
runtime. :) Or better: the first is compiled as calls to extension
methods of IQueryable<T> (and the second is also compiled as such, so
they end up as the same code) and these methods build an expression tree
at runtime.
Is it any difference in performance between these two ?

no.

If you want to know how the C# compiler converts the C# specific linq
statements to extension methods, you should read the official C#
language spec v3, I think chapter 7.
A question about understanding for the first one.
Here you return true for those that match the string literal "Rio de
Janeiro"
but then what data is this *.Select(c => c);* operate on ?
Is this *.Select(c => c);* operate on those data that is return as true
from
Where(c => c.City == "Rio de Janeiro"

It's operating on elements from the db.Customers sequence. So the
sequence db.Customers (i.e. all the customers) is passed through the
Where(c=>... ) method and only the ones matching the filter are
returned, resulting in a new sequence with elements of the same type.
Then these elements are passed through the Select method which projects
them (though the lambda doesn't really project anything so the same
elements are returned).

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Weird design decision in LinQ To SQL 7
linq queries 5
DLinq 7
[html-post] Where the Demo ends... 14
LINQ Question (Contains) 4
LINQ Question (Contains) 20
linq vs lambda 2
I cant make a full dynamic query in LINQ 2

Top