Search code examples
c#asp.netwcfwindows-store-appswcf-data-services

how to filter data from DataServiceCollection ? in WCF data service


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;
                }
            }
        }

Solution

  • 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.