nuyuljana
nuyuljana

Reputation: 59

How to incorporate criteria in Where method?

I want to ask about the WHERE condition in my search command. I'm calling web service (API) during searching and I want to put WHERE statement in my code but there's an error.

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

Both assignments of datas in the if ... else causes 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?) compilation errors:

"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

I don't know how to use the WHERE condition there. Please help me. Thank you in advance for your concern.

Upvotes: 0

Views: 72

Answers (2)

Nkosi
Nkosi

Reputation: 247213

datas is a List<Complaint> but you try to reassign it to IEnumerable<Complaint> with the Where statement. Add a ToList() after the Where to maintain type,

Or you could just declare datas as IEnumerable<Complaint>

IEnumerable<Complaint> datas = new List<Complaint>(result.Data.Items);

Upvotes: 5

AndrewP
AndrewP

Reputation: 1618

Issue is that datas is defined as being a List<Complaint>, and the return type of datas.Where(...) is an IEnumerable/IQueryable.

You could do:

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

Complete code:

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

You will then also have an error on the next line, Datas = new ObservableCollection becasue Datas is not defined, and if you meant datas, again, it will not be the List<> that you initially defined.

Upvotes: 2

Related Questions