2011-04-03 188 views
1

查询我有以下查询过滤按日期

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

感谢您的帮助。

回答

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  
        }; 
+0

这没有输出任何数据 – 2011-04-03 23:05:20

+0

好,我猜不出你的模式。 – Femaref 2011-04-03 23:10:40

+0

感谢您的代码。我现在正在工作,只需要切换日期。我已更新我的问题以包含正确的代码。 – 2011-04-04 10:09:52

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

由于这是LINQ到SQL /实体您必须先计算日期,然后在查询中使用它:

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 
... 
+0

它是3个月而不是30天。 – Aliostad 2011-04-03 22:42:56

+0

三个月当然不是30天。 – Femaref 2011-04-03 22:43:36

+0

错字 - 修正了 – BrokenGlass 2011-04-03 22:45:15