none
Limit rows returned for EntitySet not using DataLoadOption.AssociateWith RRS feed

  • Question

  • I have a simple 1-to-many relationship in my db.

     

    Is there any way to limit the rows loaded from the database to an EntitySet<T> without using the DataLoadOption.AssociateWith method?

     

    The reason I cannot use DataLoadOption is that would apply to every query against my entity within the same DataContext, which is something I don't want in this case.

     

    According to this http://msdn.microsoft.com/en-us/library/bb399410.aspx it should be quite easy, but as far as I can see it is just wrong. EntitySet<T> does not implement IQueryable<T> (the referenced doc claims it does).

    Wednesday, October 22, 2008 10:47 AM

Answers

  • The reason that DataLoadOptions applies to the whole DataContext for its entire lifetime is that you would get inconsistent or unexpected results in exactly the situation you are describing if it were not.

     

    A major guarantee of the DataContext is the object identity of the returned entities.  If two separate query results each contain the same entity, then you are guaranteed to get the same object instance back in each of them.  Can you image what would happen if the association properties were expected to be filled differently in each of the two queries?  Which set would you get? The first one?  The first one after the first query and then the second one after the second query?  The first one after the first query and then a combined list after the second query? 

     

    This is not an easy problem to solve as there is no obvious answer.  The only way to be correct & consitent is to either limit the view of the data to one shape over the lifetime of the access method (DataContext), or to never guarantee object identity across queries (creating new instances each time).  Yet this second one would be equivalent to using separate DataContext's for each query.

     

    A work around for you could be to 1) disable deferred loading on the DataContext so it never happens automatically and then 2) assign the association properties explicitly.

     

     

     

    Thursday, October 23, 2008 9:27 PM
    Moderator