具有OData $ select的AspNetCore Web API返回null

时间:2018-12-07 01:03:21

标签: c# asp.net-core odata asp.net-core-webapi

我有一个配置了Odata终结点的Web api解决方案,它与$ select以外的所有参数都可以很好地工作。

所以我为Odata支持编写了扩展,这是代码,

  private static QueryResult<T> Execute<T>(IQueryable<T> queryable, ODataQueryOptions<T> queryOptions)
        {
            try
            {
                var value = (queryOptions.ApplyTo(queryable) as IQueryable<T>);  //here returns null
                var result = value.ToList();
                int? count = null;
                if (queryOptions.Count != null && queryOptions.Count.Value)
                {
                    var countQuery = queryOptions.ApplyTo(
                    queryable,
                    AllowedQueryOptions.Skip | AllowedQueryOptions.Top | AllowedQueryOptions.OrderBy
                    ) as IQueryable<T>;
                    count = countQuery.Count();
                }
                var queryResult = new QueryResult<T>
                {
                    Value = result,
                    Count = count.HasValue ? count.Value.ToString() : null
                };
                return queryResult;
            }
            catch (System.Exception ex)
            {
                throw;
            }
        }

我按如下所示在网址中传递查询

https://localhost:44358/api/v2.0/data/clients?productId=bfe2e306-2471-455d-a2a3-4027522d5a20&$select=clientName

0 个答案:

没有答案