如何获取要通过字符串列表过滤的IQueryable项

时间:2019-06-16 05:36:58

标签: c# sql linq odata kendo-asp.net-mvc

我正在使用Odata作为我的Kendo Grid的类的IQueryable的服务。我有一个项目编号字符串列表,我需要过滤一个IQueryable类,以仅显示该字符串列表中该类的项目编号,并使其保持IQueryable。

我已经尝试使用Contains来过滤列表中的项目编号,但是我收到错误消息“不支持'Contains'方法。我还需要使其保持IQueryable。

以下是我正在使用的代码:

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;

2 个答案:

答案 0 :(得分:1)

尝试加入

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;

答案 1 :(得分:0)

您可以使用queryList代替itemQuery

var filtered = queryList.SelectMany(x=> itemQuery.Where(item => item.Number == x).ToList());