C# ASP.NET MVC-按日期范围筛选

C# ASP.NET MVC-按日期范围筛选,c#,asp.net,asp.net-mvc,visual-studio,C#,Asp.net,Asp.net Mvc,Visual Studio,我有一个日期字段“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

我有一个日期字段“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());   
}
这是我的模型公共部分类tb\U访问

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; }
public int id_visita{get;set;}
[必需]
公共int id{get;set;}
[必需]
[DisplayFormat(DataFormatString=“{0:dd/MM/yyyy}”)]
公共日期时间Fecha{get;set;}
公共可空id_g{get;set;}
公共字符串视频{get;set;}
公共可空id_fol{get;set;}
公共可空id_livros{get;set;}
公共可空id_broc{get;set;}
公共可空id_wp{get;set;}
公共字符串关系{get;set;}
公共虚拟tb_手册tb_手册{get;set;}
公共虚拟tb_despertai tb_despertai{get;set;}
公共虚拟tb_estrangeiros tb_estrangeiros{get;set;}
公共虚拟tb_folhetos tb_folhetos{get;set;}
公共虚拟tb_livros tb_livros{get;set;}
公共虚拟tb_sentinela tb_sentinela{get;set;}
使用LINQ的.Where()方法

//获取:tb\u访问
公共行动结果索引(DateTime?startdate,DateTime?enddate)
{
var tb_visitas=db.tb_visitas.Include(t=>t.tb_手册)
.Include(t=>t.tb_despertai)
.Include(t=>t.tb_estrangeiros)
.包括(t=>t.tb\u folhetos)
.包括(t=>t.tb_livros)
.包括(t=>t.tb_sentinela)

.Where(t=>t.Fecha>=startdate&&t.Fecha抱歉!我如何按日期范围筛选表?示例:筛选2018年3月1日至2018年3月9日之间的记录。为什么不能对集合执行Where操作?有效!谢谢!我如何使用此示例按“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)
      .Where(t => t.Fecha >= startdate && t.Fecha <= enddate);
    return View(tb_visitas.ToList());   
}