13.5. Practical LINQ Tips

Here are some practical LINQ tips.

  • In this chapter you saw how to create anonymous types to shape the data you want to return from your queries. The compiler and IntelliSense are invaluable tools in determining what data you can return and what properties you have available. Spend some time playing around with the anonymous types, looking at the different options that the IntelliSense lists give you.

  • Just as with other data access methods like the SqlDataSource control you saw in the previous chapter, try to filter your data as much as possible. If you know you only need reviews in the Jazz genre, be explicit and incorporate a Where clause in your code that limits the list of reviews at the database level. This speeds up your queries and data retrieval, improving the overall speed of the application.

  • Make use of anonymous types to decrease the memory consumption of your LINQ queries. Instead of retrieving the entire Review object, use the New keyword to create a new anonymous type on the fly. Since this new object only contains the properties you really need, you save yourself the overhead of bringing in the full object.

Get Beginning ASP.NET 3.5: In C# and VB now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.