ASP.NET MVC SQL

ASP.NET MVC SQL,sql,asp.net-mvc,Sql,Asp.net Mvc,我有以下代码: public IList<ProductionRuns> List() { var Shows3Months = (from s in _db.ProductionRuns where s.startDate <= DATEADD(month, 3, GETDATE()) and s.endDate > = GETDATE() select s);

我有以下代码:

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

其想法是,它将显示未来3个月的产品列表,以便将产品开始日期和结束日期与当前日期进行比较。有人能帮我修复SQL语句吗?谢谢

那不是SQL,那是LINQ的理解表达:我想你把你的语言弄糊涂了

要么留在C和您的模型中,要么在数据库上执行SQL

在C语言中,通过一些猜测来填充模型,我认为您的表达式应该是这样的:

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

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

编辑:预先计算日期,这样LINQ to SQL就不会尝试将其转换为SQL。

这不是SQL,这是LINQ的理解表达式:我想你把语言弄糊涂了

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();
        }
要么留在C和您的模型中,要么在数据库上执行SQL

在C语言中,通过一些猜测来填充模型,我认为您的表达式应该是这样的:

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

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

编辑:预先计算日期,以便LINQ to SQL不会尝试将其转换为SQL。

这将不起作用,因为LINQ to不支持AddMonthsql@archil好的,所以只需要预先计算。这将不起作用,因为linq to不支持AddMonthsql@archil好的,所以只需要预先计算一下。
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();
        }