【问题标题】:Task<ObservableCollection<AppointmentItem>> does not contain definition for whereTask<ObservableCollection<AppointmentItem>> 不包含 where 的定义
【发布时间】:2017-04-22 12:36:49
【问题描述】:

我正在尝试按保存在其中的日期过滤 observablecollection。用户将使用日历视图选择日期。

public async Task RefreshItems(bool showActivityIndicator, bool syncItems)
        {
            using (var scope = new ActivityIndicatorScope(syncIndicator, showActivityIndicator))
            {

                var items = manager.GetAppointmentItemsAsync();


                CalendarVM vm = new CalendarVM();
                calendar.SetBinding(Calendar.DateCommandProperty, nameof(vm.DateChosen));
                calendar.SetBinding(Calendar.SelectedDateProperty, nameof(vm.DateSelected));
                calendar.BindingContext = vm;

                var filtered = items.Where(appointmentItem => appointmentItem.Date == SelectedDate);
            }
        }

这是来自 AppointmentPage 类的代码,其中包含用户选择的日期和数据。

  public async Task<ObservableCollection<AppointmentItem>> GetAppointmentItemsAsync(bool syncItems = false)
        {


            try
            {

                IEnumerable<AppointmentItem> items = await appointmentTable
                                        .ToEnumerableAsync();

                return new ObservableCollection<AppointmentItem>(items);

            }
            catch (MobileServiceInvalidOperationException msioe)
            {
                Debug.WriteLine(@"Invalid sync operation: {0}", msioe.Message);
            }
            catch (Exception e)
            {
                Debug.WriteLine(@"Sync error: {0}", e.Message);
            }
            return null;

        }

这里是 dataManger 类中用于从数据库中检索数据的代码。

目前我收到此错误:

错误 CS1061 'Task>' 没有 包含“Where”的定义并且没有扩展方法“Where” 接受类型的第一个参数 'Task>' 可以找到(你是 缺少 using 指令或程序集引用?

【问题讨论】:

    标签: c# azure xamarin task observablecollection


    【解决方案1】:

    您的以下代码行:

    var items = manager.GetAppointmentItemsAsync();
    

    返回一个显然不包含WhereTask,这就是您收到错误的原因。

    请将此行更改为:

    var items = await manager.GetAppointmentItemsAsync();
    

    然后你会得到一个 ObservableCollection&lt;AppointmentItem&gt; 类型的集合,它应该有 Where 成员。

    【讨论】:

    • &lt;blush /&gt; :) #WishStackOverflowAllowedSmallerComments。顺便说一句,也请给@Alessandro Caliaro 一些爱:D。他是第一个回答这个问题的人。
    【解决方案2】:

    我尝试改变

    var items = manager.GetAppointmentItemsAsync();
    

    ObservableCollection<AppointmentItem> items = await manager.GetAppointmentItemsAsync();
    

    那么你可以尝试添加

    using System.Linq;
    

    【讨论】:

    • @glenncooper 该死!我已经回答了很多关于 SO 的问题,但没有人告诉我他们爱我……感到嫉妒
    猜你喜欢
    • 2012-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多