1

At the MSDN you can read the following:

We recommend that you not bind controls directly to an ObjectQuery. Instead, bind controls to the result of the Execute method. Binding in this manner prevents a query from being executed multiple times during binding.

and

If you prefer to work with LINQ queries, we recommend that you cast the result of the query to ObjectQuery and call the Execute method.

My question is: Is right to cast the result of a LINQ to Entity (that is at most an IQueryable) to an ObjectQuery, a more specific type??

Is that the right pattern to use when ones need bind a control to the result of a LINQ query?

When I drag a data source from the Visual Studio 2010 Data Sources windows to my WPF form, two methods are generated as explained at MSDN, but in my application I need apply a filter to the "predefinedTokensQuery" variable, and that is why I wrote a LINQ query (replacing the generated initialization of the "predefinedTokensQuery" variable) and then the compiler complain because it can't cast a LINQ query to an ObjectQuery explicitly.

The following code is how is right now in my application, it works, but I'm really worry to miss something here and not being following the recommended pattern.

private System.Data.Objects.ObjectQuery<Token> GetPredefinedTokensQuery(UnduplicateModelContainer unduplicateModelContainer)
{
    // Auto generated code

    System.Data.Objects.ObjectQuery<Unduplicate.Token> predefinedTokensQuery = (System.Data.Objects.ObjectQuery<Unduplicate.Token>)
        from token in unduplicateModelContainer.Tokens
        where token.Predefined == true
        select token;
    // To explicitly load data, you may need to add Include methods like below:
    // predefinedTokensQuery = predefinedTokensQuery.Include("Tokens.Type").
    // For more information, please see http://go.microsoft.com/fwlink/?LinkId=157380
    // Returns an ObjectQuery.
    return predefinedTokensQuery;
}

private void windowPredefinedTokens_Loaded(object sender, RoutedEventArgs e)
{

    Unduplicate.UnduplicateModelContainer unduplicateModelContainer = new Unduplicate.UnduplicateModelContainer();
    // Load data into Tokens. You can modify this code as needed.
    System.Windows.Data.CollectionViewSource predefinedTokensViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("predefinedTokensViewSource")));
    System.Data.Objects.ObjectQuery<Unduplicate.Token> predefinedTokensQuery = this.GetPredefinedTokensQuery(unduplicateModelContainer);
    predefinedTokensViewSource.Source = predefinedTokensQuery.Execute(System.Data.Objects.MergeOption.AppendOnly);
}

Thanks in advanced. Frank Abel

  • At http://social.msdn.microsoft.com/Forums/en/adodotnetentityframework/thread/60dbea1b-df66-4853-8411-6f144471f2d0 Daniel Simmons said "any time you use IQueryable against the entity framework, you will be going through ObjectQuery.", so I suppose that another related question is.... should the c# compiler recognize/transform the LINQ to Entities queries to an ObjectQuery instead of IQueryable to avoid such explicit cast? I'm a little confuse, can anybody explain in deep the relation between both types and how that affect the code I put above? –  Jan 23 '11 at 21:50
  • The "recommended" pattern would be MVVM nowadays. But Yes, take control over when the query is executed. Don't pay too much attention to auto-gen code, darg-and-drop is not used a lot. – H H Jan 23 '11 at 22:28
  • Thanks for the reply comment Henk, but what about the cast? Is right what MSDN document suggest? I mean, is right cast the result of a LINQ to Entity to a ObjectQuery? I'm reading right now Developer's Guide to Microsoft Prism, thanks again. –  Jan 24 '11 at 21:42

0 Answers0