如何从DataServiceCollection中过滤数据?在WCF数据服务中

时间:2013-11-16 11:41:53

标签: c# asp.net wcf windows-store-apps wcf-data-services

我在 DataServiceCollection EquipBookings中获取了数据,但是从这个列表中我想过滤数据,根据从数据选择器中选择的日期

为此我尝试写这个,但它不起作用:

我收到错误“cannnot implicity转换类型列表到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();
        }

完整代码是:

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

2 个答案:

答案 0 :(得分:1)

返回一个列表:

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

虽然FilterJobs方法的返回类型为DataServiceCollection<EquipBooking>,但两者之间没有隐式转换。编译器不理解如何将一个转换为另一个。

你可以这样做:

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

        return dataServiceCollection;

    }

DataServiceCollection的构造函数重载需要IEnumerable{T}(在您的情况下为IEnumerable EquipBookings作为参数。方便地,这正是EquipBookings.Where(c => c.CreatedOn == SeletedDateChanged);返回的内容。

答案 1 :(得分:0)

您需要将通用IEnumerable转换为ObservableCollection。您可以编写扩展方法来执行此转换。这里也给出了例子How to convert IEnumerable to ObservableCollection?