Comparing LINQ to SQL with LINQ to Objects

In the previous chapter, we used LINQ to query in-memory objects. Before we dive further into the world of LINQ to SQL, we first need to look at the relationships between LINQ to SQL and LINQ to Objects.

Some key differences between LINQ to SQL and LINQ to Objects are:

  • LINQ to SQL needs a Data Context object. The DataContext object is the bridge between LINQ and the database. LINQ to Objects doesn't need any intermediate LINQ provider or API.
  • LINQ to SQL returns data of type IQueryable<T> whereas LINQ to Objects returns data of type IEnumerable<T>.
  • LINQ to SQL queries are translated to SQL by way of Expression Trees, which allow them to be evaluated as a single unit, and translated to appropriate and optimal SQL Statements. LINQ to Objects queries do not need to be translated.
  • LINQ to SQL queries are translated to SQL calls and executed on the specified Database while LINQ to Objects queries are executed in the local machine memory.

The similarities shared by all aspects of LINQ are the syntax. They all use the same SQL-like syntax and share the same groups of standard query operators. From the language syntax perspective, working with a database is the same as working with in-memory objects.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset