Odata c#日期之间的动态查询

时间:2018-03-06 16:29:35

标签: c# linq odata

我需要为odata创建一个动态查询, ?http://xxx/odata/products $滤波器= CREATEDATE + GT + 2018-03-01T00:00:00%2B02:00 +和+ CREATEDATE + LT + 2018-03-31T23:59:59%2B03:00

查询应该使用当前月份的createDate过滤所有产品。 类似的东西:

$滤波器= CREATEDATE + GT + MonthFirstDay(今天)+和+ CREATEDATE + LT + MonthLastDay(今天)。

非常感谢

1 个答案:

答案 0 :(得分:-1)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.OData.Query;

namespace Business.BusinessLogic.Translators
{
    /// <summary>
    /// Convert odata query using keywords
    /// </summary>
    public class ODataQueryConverter : IODataQueryConverter
    {
        private readonly DateTime _serverNow = DateTime.UtcNow;
        private Dictionary<string, string> KeywordsDictionary { get; set; }

        /// <summary>
        /// constructor
        /// </summary>
        public ODataQueryConverter()
        {
            KeywordsDictionary = new Dictionary<string, string>
            {
                { "StartOfTheMonth", new DateTime(_serverNow.Year, _serverNow.Month, 1).ToString(Consts.IsoPattern)},
                { "EndOfTheMonth", new DateTime(_serverNow.Year, _serverNow.Month +1,1).AddSeconds(-1).ToString(Consts.IsoPattern)},
            };
        }

        /// <summary>
        /// Convert OData query option to new uri using const keywords
        /// </summary>
        /// <param name="odataQuery"></param>
        /// <returns></returns>
        public ODataQueryOptions Convert(ODataQueryOptions odataQuery)
        {
            if (odataQuery == null)
                return null;

            var odataRequestMessage = odataQuery.Request;
            var odataRequestUri = odataRequestMessage.RequestUri.ToString();
            var newRequestUriString = ConvertKeywords(odataRequestUri);

            odataRequestMessage.RequestUri = new Uri(newRequestUriString);
            var newOdataQueryOptions = new ODataQueryOptions(odataQuery.Context, odataRequestMessage);

            return newOdataQueryOptions;
        }

        public string Convert(string odataQueryString)
        {
            return ConvertKeywords(odataQueryString);
        }

        private string ConvertKeywords(string odataRequestUri)
        {
            return KeywordsDictionary.Aggregate(odataRequestUri, (current, keyword) => current.Replace(keyword.Key, keyword.Value));
        }
    }
}

//method in my BaseODataController used to query data 
//_converter is the instance of ODataQueryConverter that I got from depedency injection
public virtual IQueryable<T> Get(ODataQueryOptions<T> queryOptions)
{
   var convertedQuery = _converter.Convert(queryOptions);
   var queryable = BusinessController.Get();
   var convertedQueryable = convertedQuery.ApplyTo(queryable);
   return convertedQueryable;
}

现在来自odata的每个请求都会通过转换器查找我们在转换器字典中设置的关键字。 如果转换器找到任何它将用相应的值替换它。

现在我可以使用: http://api.dev.com/odata/entity?$滤波器=日期时间+ GT + StartOfTheMonth

它会自动转换为: ?http://api.dev.com/odata/entity $滤波器=日期时间+ GT + 2018-03-001T00:00:00.000Z

我们为关键字创建了一个事实来源,而不是让每个端点 翻译关键词本身。

如果有人知道如何使用当前的Odata版本,请在此处发布。