by Fabrice Marguerie via Fabrice's weblog on 12/5/2007 5:04:00 PM
Often, when you try to find out how to write the correct LINQ query you need, you end up being confused because it becomes too complex. In such situations, you should remember that the let clause is here to help you.
Let's see is an example from the official LINQ forum.Someone asked how to query the following XML document:
Here is one way to do it:
It's easier to isolate the "profile" and "support" elements in separate sequences using let clauses, as above. Then the select clause becomes simple to write.
As Luke Hoban explains on his blog:
With let query clauses, you can introduce a variable into scope and use it in the subsequent query clauses. Similar to local variables in a method body, this gives you a way to avoid evaluating a common expression multiple times by storing it in a variable. This can be very useful even in much simpler queries. Of course, in the query above - let is absolutely critical.
The "query above" he refers to is a ray tracer coded as a big LINQ query full of let clauses!
Cross-posted from http://LinqInAction.net
Original Post: Use the power of let in your LINQ queries
The content of the postings is owned by the respective author. CSharpFeeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on CSharpFeeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.