LINQ to SQL and Binding issues. RRS feed

  • Question

  • Filters and BindingListCollectionView dont go well together when the source is generated data form LINQ to SQL, The view is not filterable and thats why most of the people work with an ObserveableCollection instead that will retuen a sorteable ListCollectionView, however if you were to have an EntitySet in your Entity class (in other works a Forgin key relationship), when loading the object into the ObservableColletion<A>

    All the objects using CollectionViewSource.GetDefaultView(A) will return a ListCollectionView as expected ut if A had a Forgin relation with B which is loaded via the entity set

    using CollectionViewSource.GetDefaultView(A.B) will return a none filterable BindingListCollectionView

    I would like to solve this without creating a new ObserveableCollection for Every Relationsip i am about to use and re querying the database does anyone have a solution to this issue?




    Tuesday, January 27, 2009 2:16 AM

All replies

  • Well Since no one Anwerd i thought i give an exmaple to my problem. Lets say i used Linq to sql to auto generate There Tables that are related:

    public partial class Person: INotifyPropertyChanging, INotifyPropertyChanged  
      private string _name;  
      private EntitySet<Scene> _ALiases;  

    As you can see Linq will generate the One to Many Relationship through an EntitySet, This is the core of the problem, now lets say i have a UI with two Listboxes bounded in a master - Detail view thats simple enough, I create two CollectionView in XAML one for person and one for alias and bind the Alias to the Person

            <CollectionViewSource x:Key="MasterView" /> 
            <CollectionViewSource Source="{Binding Source={StaticResource MasterView}, Path=Aliases}" x:Key="DetailView" /> 

    Then i Bind an OberservebleCollection in code for the Master View.

    this.MasterViewSource = (this.FindResource("MasterView") as CollectionViewSource);  
    this.DetailsViewSource = (this.FindResource("DetailView") as CollectionViewSource);  
    this.MasterViewSource.Source = ObservebleCollection<Person>(DataContext.Person.ToList()); // using the Linq to Sql datacontext 

    up to here everything is nice and dandy, now if i want to filter the the fiest Person listbox its as simple as getting the ItemSource for it and using CollectionViewSource.GetDefaultView(person_listbox.ItemSource); then use using the view to filter the results.

    Now if i wanted to do the same thing on the Listbox That gets Bindind through the Aliases path, i cant since the underlying object of EntitySet will return a BindingListCollectionView and not a ListCollectionView, and the IBindingList implemetation would be one that doesnt support filtering.

    The only current way i see to solve this issue is by Using a Query to manually create an ObersrverbleCollection of Aliases for every Person i have, this is tedious work and given N layers is very very Proccess consuming.

    Does anyone have a solution on how i can filter the Second ListBox?





    Wednesday, January 28, 2009 1:05 AM