代码之家  ›  专栏  ›  技术社区  ›  nuyuljana

如何在Where方法中纳入标准?

  •  0
  • nuyuljana  · 技术社区  · 9 年前

    我想询问搜索命令中的WHERE条件。我在搜索期间调用web服务(API),我想在代码中放入WHERE语句,但出现了一个错误。

        private async Task CallApi(string searchText = null)     
        {           
            long lastUpdatedTime = 0;                           
            long.TryParse(AppSettings.ComplaintLastUpdatedTick, out lastUpdatedTime);
    
            var currentTick = DateTime.UtcNow.Ticks;            
            var time = new TimeSpan(currentTick - lastUpdatedTime);
    
    
            if (time.TotalSeconds > 1)          {
                int staffFk = Convert.ToInt32(StaffId);
                var result = await mDataProvider.GetComplaintList(lastUpdatedTime, mCts.Token, staffFk);
                if (result.IsSuccess)
                {
    
                    // Save last updated time
                    AppSettings.ComplaintLastUpdatedTick = result.Data.Updated.ToString();
    
                    // Store data into database
                    if ((result.Data.Items != null) &&
                        (result.Data.Items.Count > 0))
                    {
    
                        var datas = new List<Complaint>(result.Data.Items);
    
                        **if (!string.IsNullOrEmpty(searchText))
                            {
                                datas = datas.Where(i => i.Description.Contains(searchText)
                                                     && (i.SupervisorId.Equals(StaffId))
                                                    || (i.ProblemTypeName.Contains(searchText)));
                            }
                        else
                            {
                                datas = datas.Where(i => i.SupervisorId.Equals(StaffId));
                            }**
    
                        Datas = new ObservableCollection<Complaint>(datas);
                    }
                }
                else if (result.HasError)
                {
                    await mPageDialogService.DisplayAlertAsync("Error", result.ErrInfo.Message, "OK");
                }           
         }      
    }
    

    两项任务 datas 在 if ... else System.Collections.Generic.IEnumerable<ECS.Features.Complaints.Complaint>' to 'System.Collections.Generic.List<ECS.Features.Complaints.Complaint>'. An explicit conversions exists (are you missing a cast?) 编译错误:

    "Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<ECS.Features.Complaints.Complaint>' to 'System.Collections.Generic.List<ECS.Features.Complaints.Complaint>'.  An explicit conversions exists (are you missing a cast?)" compiler error

    2 回复  |  直到 9 年前
        1
  •  5
  •   Nkosi    9 年前

    datas 是一个 List<Complaint> 但是你试着把它重新分配给 IEnumerable<Complaint> Where 陈述添加 ToList() 在Where to maintain type之后,

    数据 像 IEnumerable<投诉>

    IEnumerable<Complaint> datas = new List<Complaint>(result.Data.Items);
    
        2
  •  2
  •   AndrewP    9 年前

    问题是 datas List<Complaint> 的返回类型 datas.Where(...)

    你可以做:

    datas = datas.Where(i => i.SupervisorId.Equals(StaffId)).ToList();
    

    private async Task CallApi(string searchText = null)     
        {           
            long lastUpdatedTime = 0;                           
            long.TryParse(AppSettings.ComplaintLastUpdatedTick, out lastUpdatedTime);
    
            var currentTick = DateTime.UtcNow.Ticks;            
            var time = new TimeSpan(currentTick - lastUpdatedTime);
    
    
            if (time.TotalSeconds > 1)          {
                int staffFk = Convert.ToInt32(StaffId);
                var result = await mDataProvider.GetComplaintList(lastUpdatedTime, mCts.Token, staffFk);
                if (result.IsSuccess)
                {
    
                    // Save last updated time
                    AppSettings.ComplaintLastUpdatedTick = result.Data.Updated.ToString();
    
                    // Store data into database
                    if ((result.Data.Items != null) &&
                        (result.Data.Items.Count > 0))
                    {
    
                        var datas = new List<Complaint>(result.Data.Items);
    
                        if (!string.IsNullOrEmpty(searchText))
                            {
                                datas = datas.Where(i => i.Description.Contains(searchText)
                                                     && (i.SupervisorId.Equals(StaffId))
                                                    || (i.ProblemTypeName.Contains(searchText))).ToList();
                            }
                        else
                            {
                                datas = datas.Where(i => i.SupervisorId.Equals(StaffId)).ToList();
                            }
    
                        Datas = new ObservableCollection<Complaint>(datas);
                    }
                }
                else if (result.HasError)
                {
                    await mPageDialogService.DisplayAlertAsync("Error", result.ErrInfo.Message, "OK");
                }           
         }      
    }
    

    下一行也会出现错误, Datas = new ObservableCollection 贝卡西 Datas 没有定义,如果你的意思是 数据