6

I've been really digging into LINQ, and I'm trying to hash out this lambda expression business. I'm just not seeing the benefit of some of the nuances of the syntax. Primarily, it seems to me that a lambda expression is mostly just a different way of using a Where clause. Why wouldn't I just use a Where clause then? Is the lambda expression more efficient?

Is it just another syntactical addition to draw programmers from another group to feel more comfortable in C#? Are there other better use cases for lambda expressions that I just haven't exposed to yet?

Andreas Grech
  • 105,982
  • 98
  • 297
  • 360
Jeremy Sullivan
  • 995
  • 1
  • 14
  • 25
  • Read this before asking such a questions. :) http://books.google.lv/books?id=12HliU9_D_kC&dq=pro+linq&printsec=frontcover&source=bn&hl=lv&ei=YXZHSumhH4nz_AbAtY2GCQ&sa=X&oi=book_result&ct=result&resnum=6 – Arnis Lapsa Jun 28 '09 at 13:56

4 Answers4

9

Take a look at this article: LINQ Query Syntax versus Method Syntax :

In general, we recommend query syntax because it is usually simpler and more readable; however there is no semantic difference between method syntax and query syntax. In addition, some queries, such as those that retrieve the number of elements that match a specified condition, or that retrieve the element that has the maximum value in a source sequence, can only be expressed as method calls. The reference documentation for the standard query operators in the System.Linq namespace generally uses method syntax. Therefore, even when getting started writing LINQ queries, it is useful to be familiar with how to use method syntax in queries and in query expressions themselves.

And also this question: LINQ: Dot Notation vs Query Expression

Community
  • 1
  • 1
Andreas Grech
  • 105,982
  • 98
  • 297
  • 360
1

Read this. Your LINQ queries will get turned into Lambda expressions by the compiler at run time.

1

Internally, the compiler will translate the query syntax into the more explicit lambda syntax. There is no inherent performance gain for either style and the generated code for most scenarios is almost identical to what people type out by hand.

The main difference is that with the lambda syntax you can chain in any extension method operating off and returning and IEnumerable<T>. With the query syntax, you are limited to the particular extension methods explicitly supported by the language (varies between language)

Really using or not using the query syntax is really a matter of personal preference.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
0

http://theburningmonk.com/2010/02/linq-lambda-expression-vs-query-expression/

  • Here’s an exam­ple of how you can join sequence together using Lambda and query expressions: – Sunandan Dutt Apr 13 '12 at 08:23
  • 1
    [Answers consisting of just a link are not considered useful](http://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers). You may want to edit your answer to include the relevant parts of the linked material, so that it's *here*, on the same site as the question. – AakashM Apr 17 '12 at 08:04