Search code examples
c#sqllinqodatakendo-asp.net-mvc

How to get an IQueryable Item to be filtered by a list of strings


I am using Odata as my service to get an IQueryable of a class to my Kendo Grid. I have a List of strings of Item Numbers and I need to filter one of my IQueryable classes to only show the Item Numbers from the class that are in that List of strings and keep it an IQueryable.

I have already tried to use Contains in order to filter out the Item Numbers in the list but I am getting the error message "The method 'Contains' is not supported'. I need to keep it an IQueryable also.

The following is the code that I am working with:

var service = RetailHelper.GetODataItemService();
var query = from a in service.ItemAssortment
            where a.Item_Nbr == itemNumber && a.AuditYear == 2018
            select a;
var newQuery = query.ToList();
var queryList = newQuery.Select(x => x.Assortment_Nbr.ToString()).Distinct().ToList();
//queryList now has the List of strings that I want to use as the filter

var itemQuery = from a in service.Items
                select a;
//I now need to filter itemQuery to only include all the Item Numbers that are in queryList. This is where my dilemma is.

var dsq = RetailerHelper.CreateDataServiceQuery<Item>(itemQuery, request);
//The above is where I will enter in the filtered itemQuery
var qor = dsq.IncludeTotalCount().Execute() as QueryOperationResponse<Item>
var result = new DataSoureResult() { Data = qor.ToList(), Total = Convert.ToInt32(qor.TotalCount) };
return result;

Solution

  • Try join

    var itemQuery = from a in service.ItemAssortment
                    join b in service.Items
                      on b.Number == a.Assortment_Nbr
                    where a.Item_Nbr == itemNumber && a.AuditYear == 2018
                    select b;