Chapter 4. Querying with LINQ to Entities

 

This chapter covers

  • Filtering with LINQ to Entities
  • Projecting with LINQ to Entities
  • Sorting and grouping with LINQ to Entities
  • Executing handcrafted SQL code
  • Choosing the fetching strategy

One of the most important features of Entity Framework is the LINQ to Entities query dialect. LINQ to Entities is a specialized version of LINQ that operates on Entity Framework models. LINQ to Entities isn’t a LINQ to SQL competitor—it’s the Entity Framework’s main query language, whereas LINQ to SQL is a full-featured O/RM that shipped with the .NET Framework v3.5.

In this chapter, we’ll begin by looking at how to filter data. Then we’ll cover projecting, grouping, sorting, joining, and querying within inheritance hierarchies. With this approach, you’ll gradually learn how LINQ to Entities works and understand how to obtain the same data you would get using native SQL queries. We’ll assume that you’re already familiar with LINQ. If you haven’t seen it yet, you can read appendix A to learn the basics.

What is the future of LINQ to SQL?

It’s no secret that LINQ to SQL is included in the .NET Framework v4.0 for compatibility reasons. Microsoft has clearly stated that Entity Framework is the recommended technology for data access. In the future, the Entity Framework will be developed and tightly integrated with other technologies, whereas LINQ to SQL will only be maintained and little evolved.

4.1. Filtering data

4.2. Projecting results

4.3. Grouping data

4.4. Sorting

4.5. Joining data

4.6. Querying with inheritance

4.7. Using functions

4.8. Executing handmade queries

4.9. Fetching

4.10. Summary

sitemap