C# 如何在动态linq中格式化日期文字?

C# 如何在动态linq中格式化日期文字?,c#,linq,datetime,dynamic,C#,Linq,Datetime,Dynamic,我使用动态Linq返回用户输入搜索条件的数据。除了用户选择的日期外,我的查询工作正常。我目前的代码是: StringBuilder whereClause = new StringBuilder(); if (startDate.HasValue || endDate.HasValue) { DateTime searchStartDate = startDate.HasValue ? startDate.Value :

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

        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();
        }
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;
AppendFormat(“Date>={0}&&Date使用


.Where(“Date>=@0&&Date=\“{0}\”&&Date.ToString()为什么要解析LINQ表达式中的字符串?LINQ的整个要点就是要避免这种情况

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();

根据您设置这些值的方式,它将使用零、一个或两个where子句。

动态LINQ字符串需要如下所示:

"Date >= DateTime(2015, 10, 21)"
这在下载的DynamicQuery项目中的文档中提到

注意,
DateTime
构造函数前面没有
new


我尝试了这个方法,效果很好。我正在使用Telerik针对ASP.NET AJAX的RadGrid控件。网格构建过滤器字符串,我需要将过滤器添加到查询中,以便使用LINQ to Entities在数据库中执行过滤器。问题是,生成的过滤器需要稍加修改,以便与LINQ to Entities一起工作,而不是与LINQ to Entities一起工作它正在执行一个
DateTime.Parse()
这在LINQ to Entities中不受支持。

在正常情况下,此语句没有问题。但是在某些情况下,例如,从用户输入动态生成过滤器,动态LINQ是一个有效的alernative。James,我需要在运行时建立查询,因为用户可以输入任何或没有输入各种搜索词。我忽略了其他词在我的代码段中,但我正在生成的.Where字符串只能在运行时执行。问题是,我的.Where子句中没有任何或任何数量的术语,这取决于用户输入的内容。我省略了非日期的代码段,但对于每一条用户输入,我都会检查null,并仅在需要时将相关术语附加到Where子句字符串中。我的where子句结尾可能是空的。我不想在“if not null…”部分中执行搜索,因为根据使用的搜索词,我会得到多个“if…else”分支。是否有办法将整个子句构建为字符串,然后在结尾执行它?问题是关于“dynamic linq”@MVision-问题是关于实现一个结果。Obalix,我真的需要整个.Where子句成为一个字符串,因为我在附加其他搜索词,所以子句的date元素可能不在那里。
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();
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);
"Date >= DateTime(2015, 10, 21)"