Filter a query by Date
I have the following query
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);
How would i add a where clause that would say
Where start date is in the next 3 months from today?
Thanks
Update
Working code
var now = DateTime.Ut开发者_开发技巧cNow;
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);
Thanks again for all your help.
As you can't use any DateTime.Add*
methods in linq-to-sql or entity framework, you'll have to use variables:
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
};
Since this is Linq to Sql/Entities you will have to calculate the date first, then use it in the query:
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
...
Add this line yo your query:
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))
....
精彩评论