简体   繁体   English

ASP.NET MVC SQL

[英]ASP.NET MVC SQL

I have the following code: 我有以下代码:

public IList<ProductionRuns> List()
        {
            var Shows3Months = (from s in _db.ProductionRuns
                                where s.startDate <= DATEADD(month, 3, GETDATE()) and s.endDate > = GETDATE() select s);

            return Shows3Months.ToList();
        }

The idea is that it will show a list of productions for the next 3 months so it compares against the production start date and end date with the current date. 这个想法是,它将显示接下来三个月的生产清单,以便与生产开始日期和结束日期与当前日期进行比较。 Can anyone help me fix the SQL statement? 谁能帮助我修复SQL语句? Thanks 谢谢

public IList<ProductionRuns> List()
        {
            DateTime startDate = DateTime.Today.AddMonths(-3);
            DateTime endDate = DateTime.Today;

            var Shows3Months = (from s in _db.ProductionRuns
                                where s.startDate <= startDate and s.endDate >= endDate select s);

            return Shows3Months.ToList();
        }

That's not SQL, that's a LINQ comprehension expression: I think you have your languages confused. 那不是SQL,那是LINQ理解表达式:我认为您的语言很困惑。

Either stay in C# and your model, or execute SQL on the database. 要么停留在C#和您的模型中,要么在数据库上执行SQL。

In C# (with some guessing to fill in the model) I think your expression needs to be something like: 在C#中(有一些猜测可以填充模型),我认为您的表达式需要像这样:

var d1 = DateTime.Today.AddMonths(-3);

from s in _db.ProductionRuns
where s.startDate <= d1 && s.endDate >= DateTime.Today
select s

EDIT: Pre-calculate date so LINQ to SQL doesn't try and translate it to SQL. 编辑:预先计算日期,以便LINQ to SQL不会尝试将其转换为SQL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM