views:

54

answers:

2

I am comparing the EF and typed datasets for their usefulness. I was failing to see why you would use the EF over typed datasets if the EF is bound to SQL Server only. But is it true that the Linq statements in EF are evaluated late in the respect that if you did something like:

db.Customers.where(c => c.Name == "John Smith")

The EF would build up a query like:

select * from Customers where Name = 'John smith'

But with Typed datasets you could write:

bll.GetCustomers().where(c => c.Name == "John Smith")

Which is very similar but the difference is it first runs:

select * from Customers

And then using the standard collections library finds the rows which contain the Name: "John Smith". In theory meaning the EF will be more efficient.

Is this correct?

+2  A: 

Yes. With Entity Framework, it's using IQueryable<T> to construct your queries. By doing:

var results = db.Customers.Where(c => c.Name == "John Smith");

Internally, the results will be IQueryable<Customer> (or your type). This allows the provider (EF) to optimize how that is executed internally. In the case of SQL Server, the actual query sent to the server will have the SQL WHERE clause in place already, which in turn will mean you'll only return a single record (if "Name" is unique) back from the DB instead of every record.

Using typed datasets, you'll return every record, then search the results for the appropriate Name afterwards. This is potentially much less efficient.

Reed Copsey
Great, this was just the kind of answer I was looking for :) Now to work out whether I use the EF as my data access layer or abstract it with another layer on top of it...I kind of like the idea of using Linq in my business logic layer. (But this is probably a question for later)Thanks !
James Hulse
+1  A: 

That is correct. Entity Framework is an Object-Relational Mapper (ORM). It provides a way to map your objects to a relational data for querying. IQueryable<T> is used with EF and can basically optimize the SQL that is sent to and from the server.

TheCloudlessSky