我如何格式化动态LINQ日期文字? [英] How do I format date literals in dynamic linq?

查看:97
本文介绍了我如何格式化动态LINQ日期文字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用动态LINQ到返回用户输入搜索条件的数据。我的查询工作正常,除了用户选择的日期。我当前的代码是:

I am using dynamic Linq to return data for user-input search criteria. My query is working fine except for the user selected dates. My current code is:

        StringBuilder whereClause = new StringBuilder();

        if (startDate.HasValue || endDate.HasValue)
        {
            DateTime searchStartDate = startDate.HasValue ? startDate.Value : DateTime.MinValue;
            DateTime searchEndDate = endDate.HasValue ? endDate.Value : DateTime.MaxValue;

            whereClause.AppendFormat("Date >= {0} && Date <= {1}",
                searchStartDate.Date.ToUniversalTime(),
                searchEndDate.Date.ToUniversalTime());
        }

        if (whereClause.Length > 0)
        {
            return (from p in this.repository.GetQueryable<Party>() select p)
                .Where(whereClause.ToString())
                .ToList();
        }

由于比较是被一个DateTime字段和之间进行查询倒下INT32场,这意味着查询解释我的日期文字为整数。

The query falls over because the comparison is being done between a DateTime field and a Int32 field, meaning the query has interpreted my date literals as integers.

我应该如何被格式化的日期?

How should I be formatting the dates?

推荐答案

你为什么在LINQ表达式解析字符串? 。LINQ的整点是为了避免

Why are you parsing strings in a LINQ expression? The entire point of LINQ is to avoid that.

var q =  from p in this.repository.GetQueryable<Party>() select p;

if (startDate.HasValue || endDate.HasValue) 
{ 
  var searchStartDate = startDate.HasValue ? startDate.Value : DateTime.MinValue; 
  var searchEndDate = endDate.HasValue ? endDate.Value : DateTime.MaxValue; 
  return 
         q.Where (p=> p.Date >= searchStartDate.ToUniversalTime() 
                   && p.Date <= searchEndDate.ToUniversalTime()).ToList();
} 
return q.ToList();



更新:
在回应评论:我建立一个在运行时。现在的问题是不运行时间与编译时间;它的字符串中与代码。 StringBuilder的,您可以追加文本; LINQ可以让连锁lamdbas。它一切工作出来的一样---除非你的代码是类型安全和使用lambda表达式的语法检查

UPDATE: In response to comments: I'm building that one at run-time. The question isn't run-time vs compile-time; it's "in strings" vs "in code". StringBuilder lets you append text; LINQ lets to chain lamdbas. It all works out the same --- except your code is type-safe and syntax checked using lambdas.

要进一步向世人证明这一概念,下面的代码编译和放大器;运行良好,并允许您根据值来改变Where子句 oddsOnly LOWERLIMIT

To demostrate this concept further, the following code compiles & runs fine, and allows to you to change the Where clause based on the values of oddsOnly and lowerLimit.

int[] nums = {1,2,3,4,5,6,7,8,9,10};

bool oddsOnly = true; 
bool lowerLimit = 5;

var q = from i in nums select i;

if (oddsOnly)
    q = q.Where( n=> n%2 == 1);

if (lowerLimit != 0)
    q = q.Where( n=> n >= lowerLimit);

foreach(var i in q)
    Console.WriteLine(i);

取决于你如何设置这些值,它将使用零,一个或两个where子句中。

Depending on how you set those values, it will use zero, one or both of the where clauses.

这篇关于我如何格式化动态LINQ日期文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆