Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is recommended pattern to bind a WPF control to a LINQ to Entity query?
    primarykey
    data
    text
    <p>At the <a href="http://msdn.microsoft.com/en-us/library/bb738469.aspx" rel="nofollow">MSDN</a> you can read the following:</p> <blockquote> <p>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.</p> </blockquote> <p>and</p> <blockquote> <p>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.</p> </blockquote> <p>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??</p> <p>Is that the right pattern to use when ones need bind a control to the result of a LINQ query?</p> <p>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.</p> <p>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.</p> <pre><code>private System.Data.Objects.ObjectQuery&lt;Token&gt; GetPredefinedTokensQuery(UnduplicateModelContainer unduplicateModelContainer) { // Auto generated code System.Data.Objects.ObjectQuery&lt;Unduplicate.Token&gt; predefinedTokensQuery = (System.Data.Objects.ObjectQuery&lt;Unduplicate.Token&gt;) 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&lt;Unduplicate.Token&gt; predefinedTokensQuery = this.GetPredefinedTokensQuery(unduplicateModelContainer); predefinedTokensViewSource.Source = predefinedTokensQuery.Execute(System.Data.Objects.MergeOption.AppendOnly); } </code></pre> <p>Thanks in advanced. Frank Abel</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload