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