Print Page | Close Window

Bindable LINQ query

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=2112
Printed Date: 22-Apr-2026 at 1:52pm


Topic: Bindable LINQ query
Posted By: kpozin
Subject: Bindable LINQ query
Date Posted: 31-Aug-2010 at 3:08pm
Does DevForce's LINQ-to-Entities have any support for bindable query results?
That is, if I select some entity objects using LINQ, is there any way to ensure that the collection that holds the result will implement INotifyCollectionChanged and will be bidirectionally bindable?
Is there, for example, a way to wrap a RelatedEntityList around a query?



Replies:
Posted By: sbelini
Date Posted: 07-Sep-2010 at 3:12pm
Hi,
 
DevForce's LINQ-to-Entities does support bindable query results indeed.
 
You can have bidirectional bindable query results by using ObservableCollection.
 
While the reladedEntityList is not intended to be used to wrap a query (it's intended to be used as the return value of a collection navigation property), you can do that with ObservableCollection as well.
 
A suggestion would be creating a "ToObservableCollection" extension method, so you could use like this:
 
 
##################
 
var bindableList = _em1.Customers
                       .Where(c => c.CompanyName.StartsWith("C"))
                       .ToObservableCollection();
 
##################
 
 
The extension method would look like this (please note that the code below does not do any error checking):
 
 
##################

public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> items) {
  var result = new ObservableCollection<T>();
  foreach (var each in items) {
    result.Add(each);
  }
  return result;
}

##################
 
 
You might also be interested in knowing about the EntityListManager, so you could monitor the entire cache to any changes that could affect bindableList (but made directly into it). You can find more information about the EntityListManager in the http://drc.ideablade.com/xwiki/bin/view/Documentation/TheEntityListManager - DevForce Resource Center .


Posted By: kpozin
Date Posted: 07-Sep-2010 at 3:19pm
Silvio, your code for ToObservableCollection does not seem to create any binding to the query. What will cause new items added to the Customers entity set to also appear in bindableList after the bindableList is initialized?


Posted By: sbelini
Date Posted: 07-Sep-2010 at 6:33pm
The ObservableCollection is bindable, but it only reflects the changes done to itself.
To monitor to any other changes you might want to use the EntityListManager:
 
##################
 
var filter = new Predicate<Customer>(delegate(Customer aCustomer) {return aCustomer.CompanyName.StartsWith("C");});
var elm = new EntityListManager<Customer>(mgr, filter, null);
elm.ManageList(bindableList, true);
##################
 
This way, any changes made to any Customer matching the criteria above with reflect in bindableList (regardless of if it was made in bindableList or not).



Print Page | Close Window