0

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? Thanks

2 Answers 2

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

That's not SQL, that's a LINQ comprehension expression: I think you have your languages confused.

Either stay in C# and your model, or execute SQL on the database.

In C# (with some guessing to fill in the model) I think your expression needs to be something like:

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.

2
  • This will not work as AddMonth is not supported by linq to sql
    – archil
    Apr 8, 2011 at 10:28
  • @archil OK, so just need to pre-calculate.
    – Richard
    Apr 8, 2011 at 12:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.