tags:

views:

279

answers:

7

Kindly let me know the difference between the "where" in (1) and "where()" in (2).

When to use "where" and "where()" ?

List<Person> pList = 
new List<Person>
  {  
       new Person
             {EmpNo=1,FirstName="Marc",LastName="Loel",Salary=3434},

       new Person
             {EmpNo=2, FirstName="Steve",LastName="Kaith",Salary=4545},       

       new Person 
             {EmpNo=3,FirstName="Neol",LastName="Henk",Salary=2222},          

   };

 (1) var v = from p in pList where p.EmpNo == 1 select new { p.FirstName };

 (2)  var query =pList .Where(p => p.EmpNo == 1)
                               .Select(p => new { p.FirstName});
+6  A: 

There is no real difference. The first where (and select) are a special in-language query expression that get translated by the compiler into those other lambda-based Where and Select methods.

StriplingWarrior
+2  A: 

There is no difference. Number (1) is just written with some syntactic sugar.

Quick look at the code in reflector and it looks like this:

var v = pList.Where<Person>(delegate (Person p) {
    return (p.EmpNo == 1);
}).Select(delegate (Person p) {
    return new { FirstName = p.FirstName };
});
var query = pList.Where<Person>(delegate (Person p) {
    return (p.EmpNo == 1);
}).Select(delegate (Person p) {
    return new { FirstName = p.FirstName };
});

As you can see, they are exactly the same.

empi
+2  A: 

I believe they are identical. Microsoft created the syntax in (1) for readability, but the compiler handles it as (2).

Matt Sherman
+5  A: 

It's just syntactic sugar. In fact, if you just have a class with the correct Where method, even if the class isn't enumerable, you can use the syntactic magic:

class MyClass
{
    public IQueryable<int> Where(Func<int, bool> predicate)
    {
        return Enumerable.Range(1, 100).AsQueryable();
    }
}

static void Main(string[] args)
{
    var q = from p in new MyClass()
            where p == 10
            select p;      
}

This doesn't do anything, but it builds and will call that method.

BFree
That's a neat trick. I suppose this is how additional LINQ providers get created?
StriplingWarrior
That's exactly how it's done. There's a guy that works at Microsoft that had a video of Channel 9, and used this technique to demonstrate Linq to Simpsons. Funny stuff :) He's working on Linq to Powershell at the moment. If I find that video, I'll update with a link.
BFree
+22  A: 

The difference is that one form is easier to read and the other form is more difficult to read. Trouble is, about half the people think the first one is easier, and half the people think the second one is the easier one! Choose the one you like best and stick with it; they mean exactly the same thing.

Eric Lippert
LOL. +1 for wit.
StriplingWarrior
ok, there 10 types of people again
bniwredyc
+1  A: 

The difference (if you want to be picky) is that the first one is LINQ, and the second one isn't.

LINQ is the integrated query language that you see in the first example, and the compiler turns it into using the extension methods seen in the second example.

Guffa
I disagree, the second method IS a LINQ query, only without comprehension syntax.
Pop Catalin
Why the downvote? If you don't say what it is that you don't like, it's rather pointless.
Guffa
A: 
Luiscencio
Not a very useful contribution to this thread.
TehOne
my bad =(......
Luiscencio