views:

134

answers:

1

I am developing a small application using EF 4.0 and POCO.

While testing my application, I grew concerned about the performance of the Data Access Layer. So I fired SQL Profiler to see that when trying to retrieve a record:

ctx.Orders.Include("OrderItems").FirstOrDefault<Order>(c => c.OrderID == id);

the EF issues a SQL statement that would retrieve all records from the Orders table on the Server and as such return to DAL at which time L2E would pick one thay meet the criteria and return it.

Can this behaviour be changed.

Thanks!

Zen

+3  A: 
Morteza Manavi
Thanks Morteza for your prompt reply. This works either way. I should have worded my question differently though. I have a function as follows:public List<Order> FindOrders(Func<Order, bool> predicate) { using (DBContext ctx = new DBContext()) { return ctx.Orders.Include("OrderItems").Where(predicate).ToList<Order>(); } }when I invoke this function like var order = rep.FindOrders(c => c.OrderID == id)[0] than the SQL Profiler shows an SELECT statement that does not contain a WHERE clause.Any ideas?Thanks again!Zen
UncleZen
No problem. I have edited my answer to address this issue with passing a predicate to a function. Please have a look and let me know how this one works for you.
Morteza Manavi
Thanks Morteza, for your detailed answer. I cannot wait to try this when I go home. I'll keep you updated.Zen
UncleZen
You are very welcome, I guess it worked for you since you mark it as the answer.
Morteza Manavi
Yes it worked indeed. I just tested it.I marked it as answered earlier on, because I was very confident that this was the solution. Thanks Morteza!Zen
UncleZen