按日期过滤查询



我有以下查询

var query =  
                     from f in _db.Production
                     join g in _db.Run on f.show equals g.Production.show

                     select new ViewProductions {
                            Venuename = g.venue,
                            Showname = f.show,
                            StartDate = g.startDate,
                            EndDate = g.endDate

                     };
        return View(query);

我该如何添加where子句来表示

从今天起3个月内的起始日期在哪里?

感谢

工作代码
var now = DateTime.UtcNow;
        var limit = now.AddDays(90);
        var query =
        from f in _db.Production
        join g in _db.Run on f.show equals g.Production.show
        where g.endDate >= now && g.startDate <= limit

                    select new ViewProductions
                    {
                        Venuename = g.venue,
                        Showname = f.show,
                        StartDate = g.startDate,
                        EndDate = g.endDate
                    };

        return View(query);

再次感谢您的帮助。

由于您不能在linq-to-sql或实体框架中使用任何DateTime.Add*方法,因此您必须使用变量:

var now = DateTime.UtcNow;
var limit = now.AddDays(90);
var query =  from f in _db.Production
             join g in _db.Run on f.show equals g.Production.show
              where g.StartDate >= now && g.StartDate <= limit
              select new ViewProductions {
                      Venuename = g.venue,
                      Showname = f.show,
                      StartDate = g.startDate,
                      EndDate = g.endDate    
                     };

由于这是Linq to Sql/Entities,您必须首先计算日期,然后在查询中使用它:

DateTime futureDate = DateTime.Now.AddMonths(3);
from f in _db.Production
join g in _db.Run on f.show equals g.Production.show
where g.StartDate <= futureDate && g.StartDate >= DateTime.Now
...

将这行添加到您的查询中:

var query =  
from f in _db.Production
join g in _db.Run on f.show equals g.Production.show
where g.startDate > DateTime.Today && 
g.startDate < DateTime.Today.Add(TimeSpan.FromDays(90))
 ....

相关内容

  • 没有找到相关文章

最新更新