0
int[] integers = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

What is the difference between this :

var odd = from i in integers
          where i % 2 == 1
          select i;

and this :

var ODD = integers.Where(i => i % 2 == 1);

if there is no difference and just the faces are different, so why should it be possible at all? I mean what is the need of having two ways of doing it?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Mahdi Tahsildari
  • 13,065
  • 14
  • 55
  • 94

4 Answers4

2

Nothing - the first is syntactic sugar for the second.

I use what ever makes the intention clear. Sometimes terseness is fine, sometimes the flow of the fluent methods, and at other times a query to express what I'm doing.

Preet Sangha
  • 64,563
  • 18
  • 145
  • 216
0

There is no difference, it's just different syntax. Take a look at the LINQ docs

Zbigniew
  • 27,184
  • 6
  • 59
  • 66
0

The compiler specification requires that LINQ queries be translated into extension method calls before being compiled.

The LINQ queries are generally more readable that the extension method calls. That's all there is to it.

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • 1
    I'd argue the opposite. Being fluent in SQL makes learning Linq comprehension syntax a real PITA, because they're similar but also substantially different. Unless I'm doing joins, I far prefer the extension methods. – spender Jul 01 '12 at 13:58
0

Indeed, you are talking about the difference between query expression and method chain.....There is no any difference in performance wise

For more info, check out this post

Community
  • 1
  • 1
Akash KC
  • 16,057
  • 6
  • 39
  • 59