views:

20

answers:

1

First let me start by explaining my use case:

Say there is a database "Cars". In that database, each row might have Make, Model, EngineType, etc. I have a page that is essentially a view of a single "Car" record, displaying its various stats. In that page, I have a user control that calculates and displys various MPG stats based on the "Car" record. The user control is passed the "Car" record via a property, then uses it internally for its calculations.

Inside that user control, something like this happens:

private void DataBindGrid()
{
    gridView.DataSource = this.CarRecord.GetMPGStats();
}

Internal to "CarRecord", is a LINQ-to-SQL query, using the data context of CarRecord. For the purpose of this calculation, it would be more efficient to do this:

private void DataBindGrid()
{
    DataLoadOptions dlo = new DataLoadOptions();

    dlo.LoadWith<Car>(c => c.Engine);

    this.CarRecord.DataContext.LoadOptions = dlo;

    gridView.DataSource = this.CarRecord.GetMPGStats();
}

For the purpose of this example, ignore whether or not this may be a bad design for a user control passing in a record & datacontext.

Here are the issues I'm seeing:

  • The page may have set its own load options before passing the record to the user control, resulting in an inefficient query.
  • The page may not want the new settings specified by the user control when it continues to use the Car record itself.

So I have two questions:

  1. What is the best way to "clear" LoadOptions? Simply set DataContext.LoadOptions = new DataLoadOptions(); or = null;?
  2. Is there any way to set some sort of temporary LoadOptions that only affect a certain operation before reverting to whatever they were before?

Thanks in advance.

+1  A: 

data load options always have to be set before execution of first query. once a query executes there is nothing u can do with them.

Muhammad Adeel Zahid