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

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This will not work as AddMonth is not supported by linq to sql
@archil OK, so just need to pre-calculate.
2
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();
        }

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.