views:

101

answers:

1

In a repository, I do this:

public AgenciesDonor FindPrimary(Guid donorId) {
    return db.AgenciesDonorSet.Include("DonorPanels").Include("PriceAdjustments").Include("Donors").First(x => x.Donors.DonorId == donorId && x.IsPrimary);
}

then down in another method in the same repository, this:

AgenciesDonor oldPrimary = this.FindPrimary(donorId);

In the debugger, the resultsview shows all records in that table, but:

oldPrimary.Count(); 

is 1 (which it should be).

Why am I seeing all table entries retrieved, and not just 1? I thought row filtering was done in the DB.

If db.EntitySet really does fetch everything to the client, what's the right way to keep the client data-lite using EF? Fetching all rows won't scale for what I'm doing.

A: 

You will see everything if you hover over the AgenciesDonorSet because LINQ to Entities (or SQL) uses delayed execution. When the query is actually executed, it is just retrieving the count.

If you want to view the SQL being generated for any query, you can add this bit of code:

var query = queryObj as ObjectQuery; //assign your query to queryObj rather than returning it immediately

if (query != null)
{
    System.Diagnostics.Trace.WriteLine(context);
    System.Diagnostics.Trace.WriteLine(query.ToTraceString());
}
mkedobbs
But why would the query ever return all rows in the table, when there is only 1 row that matches the lamba predicates? That's what's bothering me. It's almost like EntitySet snapshots a copy of the entire table into EF, and then EF somehow does the filtering outside of the database. This won't work for my needs, as I have hundreads of thousands of rows.
Dale
You are causing the statement to be enumerated in the debugger, which does not reflect what is actually happening in the code. When the code executes, only one row will be retrieved from the database.
mkedobbs