using System;
using System.Linq;
using System.Data.Linq;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace WPCompiledQuery
{
    public class PersonenDataContext : DataContext, INotifyPropertyChanged
    {
        public static string DBConnectionString = "Data Source=isostore:/Personen.sdf";

        public PersonenDataContext(string connectionString)
            : base(connectionString)
        {
            if (!DatabaseExists())
            {
                CreateDatabase();
            }
        }

        public Table<Person> PersonenTable;

        public static Func<PersonenDataContext, string, IQueryable<Person>>
            FilteredPersons =
                CompiledQuery.Compile((PersonenDataContext context, string filter) =>
                 from p in context.PersonenTable
                 where p.LastName.Contains(filter)
                 select p);

        private ObservableCollection<Person> personen;
        public ObservableCollection<Person> Personen
        {
            get
            {
                return personen;
            }
            set
            {
                if (personen != value)
                {
                    personen = value;
                    NotifyPropertyChanged("Personen");
                    
                }
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }
}
