none
Temporarily enable deferred loading? RRS feed

  • Question

  • Hello,

    I've got a result set that I want to 1) include two levels of children in the result, and 2) get the record count.  I have a data context that is given to the function I'm working in with the LoadWith options set, though I can change that.  I'm doing pagination, so I need to get the total number of records, then use skip and take to get the records I want.

    void
    
     OuterFunction(Paginator myPaginator)
    {
        IEnumerable<Type1> results;
        using
    (MyDataContext db = this
    .CreateDataContext())
        {
            DataLoadOptions dlo = new
     DataLoadOptions();
            dlo.LoadWith<Type1>(t1 => t1.Type2);
            dlo.LoadWith<Type2>(t2 => t2.Type3);
            db.LoadOptions = dlo;
            
            // ...
    
            
    Int32 count; results = this .MyFunction( myPaginator , count out , db ).ToList(); } DisplayToUser(result, count); } IQueryable<MyFunction>( Paginator myPaginator , Int32 count out , MyDataContext db ) { IQueryable<Type1> myType1s = db.FunctionThatGetsType1s(); count = myType1s.Count(); // this does joins to Type2 // and Type3 tables and takes //forever. return myPaginator.Paginate(myType1s); }

    Is there a way that I can have the LoadWith not apply for the call to Count?

     

    Thanks!

    • Edited by Don 01001100 Friday, March 26, 2010 2:47 PM Typo in code.
    Friday, March 26, 2010 2:47 PM

Answers

  • Hello,

    I would create a new MyDataContext instance, and set its LoadOptions to null before pass the instance to the MyFunction method. 

    Please also remember that once a query is returned by a DataContext, we cannot set its LoadOption property again. 

    Besides, I am a little confused that why the Count() method will cause a dead loop.  Could you provide us with more detailed information?


    Best Regards,
    Lingzhi Sun


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, March 31, 2010 9:01 AM
    Moderator
  • Hello,

    I would create a new MyDataContext instance, and set its LoadOptions to null before pass the instance to the MyFunction method. 

    Please also remember that once a query is returned by a DataContext, we cannot set its LoadOption property again. 

    Besides, I am a little confused that why the Count() method will cause a dead loop.  Could you provide us with more detailed information?


    Best Regards,
    Lingzhi Sun


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.


    I was being hyperbolic when I said it runs forever.  It runs for a very, very long time, but it eventually completes.

    I have a Paginator<T> class that takes in an IQuerable<T>, a page number, the number of records per page, some selectors for sorting, etc.  It then does several things, including getting the total record count in the IQueryable, sorting by any provided selectors, and picking the records that are related.  My issue is that the call to IQuerable<T>.Count() performs all of the load options, even though (in my case) there's no reason to.  The record count doesn't change, regardless of whether you join out to the other types or not.

    I wrote the paginator class so that I could pick at the UI level what pagination should occur, then pass the object down to the lower levels.

    Thanks for your reply.  I appreciate the help.

    Regards,

        Don

    Thursday, April 1, 2010 3:32 PM

All replies

  • Hello,

    I would create a new MyDataContext instance, and set its LoadOptions to null before pass the instance to the MyFunction method. 

    Please also remember that once a query is returned by a DataContext, we cannot set its LoadOption property again. 

    Besides, I am a little confused that why the Count() method will cause a dead loop.  Could you provide us with more detailed information?


    Best Regards,
    Lingzhi Sun


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, March 31, 2010 9:01 AM
    Moderator
  • Hello,

    I would create a new MyDataContext instance, and set its LoadOptions to null before pass the instance to the MyFunction method. 

    Please also remember that once a query is returned by a DataContext, we cannot set its LoadOption property again. 

    Besides, I am a little confused that why the Count() method will cause a dead loop.  Could you provide us with more detailed information?


    Best Regards,
    Lingzhi Sun


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.


    I was being hyperbolic when I said it runs forever.  It runs for a very, very long time, but it eventually completes.

    I have a Paginator<T> class that takes in an IQuerable<T>, a page number, the number of records per page, some selectors for sorting, etc.  It then does several things, including getting the total record count in the IQueryable, sorting by any provided selectors, and picking the records that are related.  My issue is that the call to IQuerable<T>.Count() performs all of the load options, even though (in my case) there's no reason to.  The record count doesn't change, regardless of whether you join out to the other types or not.

    I wrote the paginator class so that I could pick at the UI level what pagination should occur, then pass the object down to the lower levels.

    Thanks for your reply.  I appreciate the help.

    Regards,

        Don

    Thursday, April 1, 2010 3:32 PM