0

I got data in DataServiceCollection EquipBookings , but from this list i want to filter data , as per date selected from data picker

for this i try to write this, but it is not working :

i got error "cannnot implicity convert type list to Dataservicecollection

  private DateTime _seletedDateChanged;
        public DateTime SeletedDateChanged
        {
            get { return _seletedDateChanged; }
            private set
            {
                _seletedDateChanged = value; 

                         // here i filter collections

                EquipBookings = FilterJobs(_seletedDateChanged);
                NotifyPropertyChanged("SeletedDateChanged");
            }
        }

        public DataServiceCollection<EquipBooking> FilterJobs(DateTime SeletedDateChanged)
        {
            return EquipBookings.Where(c => c.CreatedOn == SeletedDateChanged).ToList();
        }

Full code is :

        #region EquipBookings
        // Define the binding collection for EquipBookings.
        private DataServiceCollection<EquipBooking> _equipBookings;

        public DataServiceCollection<EquipBooking> EquipBookings
        {
            get { return _equipBookings; }
            private set
            {
                _equipBookings = value;
                _equipBookings.LoadCompleted += OnEquipBookingLoaded;
                NotifyPropertyChanged("EquipBookings");
            }
        }
        public void LoadEquipBookingsData()
        {
            _context = new THA001_devEntities(_rootUri);
            EquipBookings = new DataServiceCollection<EquipBooking>(_context);
            var query = _context.EquipBooking.Expand("Status").Where(x => x.Status.Description.ToLower() == "confirmed").OrderBy(d => d.BookedFromDteTme);
            EquipBookings.LoadAsync(query);
            IsDataLoaded = true;
        }

        private void OnEquipBookingLoaded(object sender, LoadCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                IsDataLoaded = false;
                if (EquipBookings.Continuation != null)
                {
                    EquipBookings.LoadNextPartialSetAsync();
                    EquipBookingList = EquipBookings;
                }
            }
        }
patel
  • 635
  • 5
  • 22
  • 40

2 Answers2

1

This returns a list:

return EquipBookings.Where(c => c.CreatedOn == SeletedDateChanged).ToList();

While the return type of your FilterJobs method is DataServiceCollection<EquipBooking> and there's no implicit conversion between the two. The compiler doesn't understand how to convert one to the other.

You could do something like this:

public DataServiceCollection<EquipBooking> FilterJobs(DateTime SeletedDateChanged)
    {
        var equipBookings = EquipBookings.Where(c => c.CreatedOn == SeletedDateChanged);
        var dataServiceCollection = new DataServiceCollection<EquipBooking>(equipBookings);

        return dataServiceCollection;

    }

There's a constructor overload of DataServiceCollection that takes an IEnumerable{T} (an IEnumerable of EquipBookings in your case) as a parameter. Conveniently this is exactly what the EquipBookings.Where(c => c.CreatedOn == SeletedDateChanged); returns.

Aage
  • 5,932
  • 2
  • 32
  • 57
  • i got error : An item could not be added to the collection. When items in a DataServiceCollection are tracked by the DataServiceContext, new items cannot be added before items have been loaded into the collection. – patel Nov 16 '13 at 12:12
  • @vb1 Changed the example. Could you try that? – Aage Nov 16 '13 at 13:05
0

You need to convert generic IEnumerable to ObservableCollection. You can write an extension method to do this conversion. Examples are given here as well How to convert IEnumerable to ObservableCollection?

Community
  • 1
  • 1
Himanshu Kumar
  • 541
  • 5
  • 14