按日期过滤查询

时间:2011-04-03 22:35:20

标签: linq entity-framework ado.net

我有以下查询

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

再次感谢您的帮助。

3 个答案:

答案 0 :(得分:2)

由于这是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
...

答案 1 :(得分:2)

由于您无法在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    
                     };

答案 2 :(得分:0)

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

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))
 ....