Which one is better lambda expressions or?

They are equivalent. They don't just return the same result set - they compile to the same code Use query expressions or dot notation for individual cases depending on readability. For joins, I find the dot notation quite cumbersome - but I use it for situations where I only have one or two clauses (usually where/select).

Even with two clauses (usually where and select) I find dot notation nice if you then need to use it anyway. For example, I like: var query = people. Where(person => person.

Age > 18) . Select(person => person. Name) .

Skip(100) . Take(10) over var query = (from person in people where person. Age > 18 select person.Name) .

Skip(100) . Take(10) For more complicated queries (e.g. Joins) I'd probably just separate the two: var baseQuery = from person in people where person. Age > 18 join company on person.

CompanyId equals company. CompanyId select new { person. Name, company.

Name }; var fullQuery = baseQuery. Skip(100) . Take(10) I just find this separation makes it easier to read I believe it's really useful for developers to understand at least the basics of what query expressions do - the fact that they're basically translations into dot notation, and that the language itself doesn't know anything about LINQ to Objects, LINQ to SQL etc; it's just a case of following an appropriate pattern.

It's a great bit of design, which means query expressions only affect one little bit of the language specification.

They are equivalent. They don't just return the same result set - they compile to the same code. Use query expressions or dot notation for individual cases depending on readability.

For joins, I find the dot notation quite cumbersome - but I use it for situations where I only have one or two clauses (usually where/select). Even with two clauses (usually where and select) I find dot notation nice if you then need to use it anyway. For example, I like: var query = people.

Where(person => person. Age > 18) . Select(person => person.Name) .

Skip(100) . Take(10); over var query = (from person in people where person. Age > 18 select person.

Name) . Skip(100) . Take(10); For more complicated queries (e.g. Joins) I'd probably just separate the two: var baseQuery = from person in people where person.

Age > 18 join company on person. CompanyId equals company. CompanyId select new { person.

Name, company.Name }; var fullQuery = baseQuery. Skip(100) . Take(10); I just find this separation makes it easier to read.

I believe it's really useful for developers to understand at least the basics of what query expressions do - the fact that they're basically translations into dot notation, and that the language itself doesn't know anything about LINQ to Objects, LINQ to SQL etc; it's just a case of following an appropriate pattern. It's a great bit of design, which means query expressions only affect one little bit of the language specification.

1 Great explanation :) – Waheed Aug 20 '09 at 10:48.

There's no better one. Use what you like. I would, in this case, go with the 'query' syntax, since I think it's more readable than the second one.

Moreover, since we're writing SQL anyway, I think this syntax resembles it better. But I'm sure others will disagree and tend to choose the lamda version. I usually go with the latter syntax btw, since I prefer the lambda syntax cause it is often more readable and shorter.

See also this SO question.

1 The lambda expression is not shorter. If I have more joins then it become very very long. I think you will agree with me.

– Waheed Aug 20 '09 at 10:41 Yes, I agree. That's why I said '(...) is often more readable and shorter' :) – Razzie Aug 20 '09 at 10:46.

I'd always go for the more readable option and in this case I think its the LINQ snippet.

Both will perform equally well, but I will prefer the first since it's easier to read and understand.

Write your code, then measure it to find out what to refactor. " Have you measured both? Better is subjective.

Are you finding a problem with either one that is making you want to choose one over the other?

There IS one little difference between the two types of syntax. The query expression one will take longer to compile because the compiler has to transform it into the dot syntax. Of course, it is almost completely immaterial because: the compilation overhed is minimal, the emitted IL that runtime needs to deal with is exactly the same for both cases.

I use Lambda Expressions when I have to build query dynamically, and LINQ to SQL when query is fully known. IQueryable query; if (!string. IsNullOrEmpty(regioncode)) { if (!string.

IsNullOrEmpty(compcode)) { query = db. IncomeDetailsEntities. Where(i => i.

RegionCode == regioncode && i. CompanyCode == compcode); } else { query = db. IncomeDetailsEntities.

Where(i => i. RegionCode == regioncode); } } else { query = db. IncomeDetailsEntities; } return query.

Select(i => new { i. RegionCode, i. Budget }).

There's only one way to find out: FIIIIIIIIIIIIIIIIGHT!

Use query expressions or dot notation for individual cases depending on readability. For joins, I find the dot notation quite cumbersome - but I use it for situations where I only have one or two clauses (usually where/select). Even with two clauses (usually where and select) I find dot notation nice if you then need to use it anyway.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions