0

I have following Linq query. What I want is if I do search for a particular year, I should get corresponding records of that year. Else just show records of current year. Same way searchtext has also to be done.

    public ActionResult Search(string searchtext, int? year)
    {
                string selyear = year.ToString();
                string curyear = (DateTime.Now.Year).ToString();

                hdms = from t in db.HOLIDAY                           
                       where    
                           (year == null || t.DOH.StartsWith(selyear)) &&
                           (searchtext == "" || t.HOLIDAY_NAME == searchtext)                      
                       select new HOLIDAYDETAILS
                       {
                           DOH = t.DOH,                               
                       };
                       ....
    }

I want to satisfy this also if (year == null) then t.DOH.StartsWith(curyear) and

if (searchtext == "") then t.HOLIDAY_NAME != "Sunday" && t.HOLIDAY_NAME != "Saturday".

Also If both are null at the same time, both conditions should be satisfied together.

How can I give this one too on the above where clause?

2 Answers 2

3

If checking year is null or not is your problem then you simply can do like this-

public ActionResult Search(int? year)
    {
                string selyear = year.ToString();
                string queryyear = (string.IsNullOrEmpty(selyear)) // checking for null
                                   ? (DateTime.Now.Year).ToString() // current year
                                   : selyear; // year in query

                hdms = from t in db.HOLIDAY
                       join t1 in db.EMPLOYEE on t.UPDATED_BY equals t1.EMP_CODE
                       where    
                           t.DOH.StartsWith(queryyear)  // comparing queryyear        

                       orderby
                           t.DOH
                       select new HOLIDAYDETAILS
                       {
                           DOH = t.DOH,                               
                       };
                       ....
    }

Hope this will works for you...

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

Comments

2
public ActionResult Search(int? year)
{
    string curyear = (DateTime.Now.Year).ToString();
    string selyear = year!=null? year.ToString() : curyear;

    var query = from t in db.HOLIDAY
                join t1 in db.EMPLOYEE on t.UPDATED_BY equals t1.EMP_CODE
                select new {t,t1};

    query = query.Where(o=>o.t.DOH.StartsWith(selyear));

    if (searchtext == "")
    {
        query = query.Where(o=>o.t.HOLIDAY_NAME != "Sunday" && o.t.HOLIDAY_NAME != "Saturday");
    }

    hdms = query.OrderBy(o=>o.t.DOH).Select(o=> new HOLIDAYDETAILS
           {
               DOH = o.t.DOH,                               
           });
   ....
}

2 Comments

Above code worked for me. Can you give me the condition for searchtext too?
@LimnaD'silva you can create query step-by-step depending on your conditions

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.