using System;
using System.Linq;
using System.Data.Services.Client;

namespace SilverlightApplication1.ServiceReference1
{
    public partial class NorthwindEntities
    {
        public NorthwindEntities()
            : base(new Uri("http://services.odata.org/Northwind/Northwind.svc"))
        {
            //
        }

        DataServiceCollection<Customer> _CustomersCollection;
        public DataServiceCollection<Customer> CustomersCollection
        {
            get
            {
                if (_CustomersCollection == null)
                {
                    _CustomersCollection = new DataServiceCollection<Customer>(this);
                    _CustomersCollection.CollectionChanged += (s, e) =>
                    {
                        if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
                        {
                            foreach (var c in e.NewItems.OfType<Customer>())
                            {
                                c.Context = this;
                            }
                        }
                        else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove)
                        {
                            foreach (var c in e.OldItems.OfType<Customer>())
                            {
                                c.Context = null;
                            }
                        }
                    };
                    _CustomersCollection.LoadAsync(this.Customers);
                }
                return _CustomersCollection;
            }
        }
    }
}
