【问题标题】:ASP.NET MVC - Filter By Date RangeASP.NET MVC - 按日期范围过滤
【发布时间】:2018-03-26 19:35:05
【问题描述】:

我有一个日期字段“Fecha”。我需要使用日期范围和此字段过滤我的表格。

这是我的控制器:

// GET: tb_visitas
public ActionResult Index(DateTime? startdate, DateTime? enddate)
{
    var tb_visitas = db.tb_visitas.Include(t => t.tb_brochuras).Include(t => t.tb_despertai).Include(t => t.tb_estrangeiros).Include(t => t.tb_folhetos).Include(t => t.tb_livros).Include(t => t.tb_sentinela);
    return View(tb_visitas.ToList());   
}

这是我的模型public partial class tb_visitas

public int id_visita { get; set; }
[Required]
public int id { get; set; }
[Required]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime Fecha { get; set; }
public Nullable<int> id_g { get; set; }
public string videos { get; set; }
public Nullable<int> id_fol { get; set; }
public Nullable<int> id_livros { get; set; }
public Nullable<int> id_broc { get; set; }
public Nullable<int> id_wp { get; set; }
public string Relatorio { get; set; }

public virtual tb_brochuras tb_brochuras { get; set; }
public virtual tb_despertai tb_despertai { get; set; }
public virtual tb_estrangeiros tb_estrangeiros { get; set; }
public virtual tb_folhetos tb_folhetos { get; set; }
public virtual tb_livros tb_livros { get; set; }
public virtual tb_sentinela tb_sentinela { get; set; }

【问题讨论】:

  • 对不起!如何按日期范围过滤我的表格?示例:过滤 01/03/2018 和 09/03/2018 之间的记录。
  • 为什么不能对集合进行 Where 操作?

标签: c# asp.net asp.net-mvc visual-studio


【解决方案1】:

使用 LINQ 的 .Where() 方法。

// GET: tb_visitas
public ActionResult Index(DateTime? startdate, DateTime? enddate)
{
    var tb_visitas = db.tb_visitas.Include(t => t.tb_brochuras)
      .Include(t => t.tb_despertai)
      .Include(t => t.tb_estrangeiros)
      .Include(t => t.tb_folhetos)
      .Include(t => t.tb_livros)
      .Include(t => t.tb_sentinela)
      .Where(t => t.Fecha >= startdate && t.Fecha <= enddate);
    return View(tb_visitas.ToList());   
}

https://docs.microsoft.com/en-us/dotnet/standard/using-linq

【讨论】:

  • 有效!谢谢!我如何使用这个例子订购“Fecha”?
猜你喜欢
  • 2017-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-25
  • 2018-06-19
  • 1970-01-01
  • 1970-01-01
  • 2019-03-19
相关资源
最近更新 更多