1

I use this code for advance search

public ActionResult AdvanceSearch(string HeadTitle, string fulltext, string firstdate, string enddate, string newstype)
    {
        var q = db.Tbl_News.Where(n => n.HeadTitle.Contains(HeadTitle) && n.FullText.Contains(fulltext) && n.Date >= firstdate && n.Date <= enddate);
        return PartialView("AdvanceSearch", q);
    }

this code work for Search in HeadTitle and full text but when I add date show me an error .

How use this code for search between two dates ?

2
  • 1
    Start by making your parameters DateTime (not string) Commented Sep 17, 2016 at 11:14
  • @StephenMuecke thanks . its work Commented Sep 18, 2016 at 17:17

2 Answers 2

1

First, your date parameters should be of type DateTime, and I suggest updating your code to be like

var q = db.Tbl_News
            .Where (n => n.HeadTitle.Contains(HeadTitle) 
            .Where (n => n.FullText.Contains(fulltext)) 
            .Where (n => n.Date >= firstdate.Date )
            .Where (n => n.Date <= enddate.Date);

to be more readable, and compare just the date part (ignore time)

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

Comments

1

If you don't want to change the parameter type, this is also fine.

public ActionResult AdvanceSearch(string HeadTitle, string fulltext, string firstdate, string enddate, string newstype)
{
    var q = db.Tbl_News
        .Where (n => n.HeadTitle.Contains(HeadTitle) 
        .Where (n => n.FullText.Contains(fulltext)) 
        .Where (n => n.Date >= DateTime.ParseExact(firstdate, "MM/dd/yyyy", CultureInfo.InvariantCulture))
        .Where (n => n.Date <= DateTime.ParseExact(enddate, "MM/dd/yyyy", CultureInfo.InvariantCulture));
}

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.