按日期和时间过滤C#

时间:2012-07-26 09:56:44

标签: c# asp.net

我正在使用C#ASP.Net创建报告,在报告中我可以过滤在两个不同日期之间创建的数据(例如,开始日期:7月15日和结束日期:7月17日),但是当涉及到过滤创建的数据时特定日期(例如StartDate:7月15日和结束日期:7月15日)查询不会检索任何内容......

//Code from OutletDistributor.aspx.cs
ReportManager master = ObjectFactory.GetInstance<ReportManager>();
ReportResponse responce = new ReportResponse();
if (ddDistributor == Guid.Empty)
  responce = master.GetAllOutletDistributor(sDate, EDate);
else
  responce = master.GetOutletDistributor(sDate, EDate, ddDistributor);
ReportViewer1.Reset();
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.ProcessingMode = ProcessingMode.Local;
var stream = Assembly.GetAssembly(typeof(SampleReport)).GetManifestResourceStream(responce.ReportResource);
ReportDataSource reportDataSource = new ReportDataSource(responce.ReportDataSet, responce.ReportDataSource);
stream = RdlcReportHelper.TranslateReport(stream);
ReportViewer1.LocalReport.DataSources.Add(reportDataSource);
ReportViewer1.LocalReport.LoadReportDefinition(stream);
ReportViewer1.LocalReport.DisplayName = ResourceHelper.GetText(null, "hq.rpt.outletdist.heading");
ReportViewer1.LocalReport.Refresh();

//Code from ReportManager.cs
//Filter All Outlet Distributor
public ReportResponse GetAllOutletDistributor(DateTime StartDate, DateTime EndDate) {
  ReportResponse report = new ReportResponse();
  report.ReportResource = "Distributr.HQ.Lib.Reports.RcdlFiles.OutletDistributor.rdlc";
  List<OutletReportItem> Report = _outletReport.GetAllOutlets()
    .Where(p => p.CreationDate >= StartDate && p.CreationDate <= EndDate).ToList();
  Report.ForEach(n => report.ReportDataSource.Add(n));
  report.ReportDataSet = "dsOutletDistributor";
  return report;
}

3 个答案:

答案 0 :(得分:1)

我认为StartDateEndDate都包含时间组件。使用Date属性删除它:

StartDate = StartDate.Date;
EndDate = EndDate.Date;

// your query goes here...

答案 1 :(得分:0)

在您的日期相同的情况下,在查询中使用“=”(而不是差异)

答案 2 :(得分:0)

实际上,你的代码List<OutletReportItem> Report = _outletReport.GetAllOutlets().Where(p => p.CreationDate >= StartDate && p.CreationDate <= EndDate).ToList()在初始化对象DateTime时所以默认值为date,小时,分钟和秒为'00'。所以我认为你可以使用DateTime.Compare()