按日期过滤查询

发布于 2024-10-30 18:53:09 字数 1189 浏览 1 评论 0原文

我有以下查询:

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

再次感谢您的所有帮助。

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

Thanks again for all your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清浅ˋ旧时光 2024-11-06 18:53:09

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

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    
                     };
暖阳 2024-11-06 18:53:09

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

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
...
Bonjour°[大白 2024-11-06 18:53:09

添加此行您的查询:

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

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))
 ....
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文