tags:

views:

579

answers:

3

Hello.

I trying to check if a date is this date or bigger inside my linq query with boolean. But it's not working like i want.

This my code

        public bool CheckMonth(int month)
    {
            if (month > System.DateTime.Now.Month)
            {
                return true;
            }
            else if (month == System.DateTime.Now.Month)
            {
                return true;
            }
            else
            {
                return false;
            }
    }

    public virtual IList<DateItem> GetThreeDateToList()
    {
        var data = new ScoutDataDataContext();

        var q = (from d in data.DateDetails
                 where d.Activate == 1 && CheckMonth(d.EndDate.Month) 
                 orderby d.Date.Date.Month descending
                 select new DateItem
                 {
                     Title = d.Title,
                     Date = d.Date.Date + " - " + d.EndDate.Date,
                     Link = d.Link,
                 }).Take(3);

        return q.ToList();
    }

Anyone who nows a diffrent way?

A: 

What is the provider here? LINQ-to-SQL? Objects? Entity Framework? (from the name, it looks like LINQ-to-SQL...)

Indeed, database providers won't be able to process your method; you have a few options:

  • inline it: && d.EndDate.Month >= System.DateTime.Now.Month
  • map it to a UDF (LINQ-to-SQL only) that accepts a DateTime

The first is probably easier... if you get problems with it not recognising System.DateTime.Now.Month, then do that first into a variable.

Marc Gravell
Linq-to-Sql is that i use
Frozzare
A: 

This is only checking that the month is greater than the current month, so July 4th, 1776 would pass. I assume you want to check the entire date? Do this:

    var q = (from d in data.DateDetails
             where d.Activate == 1 && d.EndDate > DateTime.Now) 
             orderby d.Date.Date.Month descending
             select new DateItem
             {
                 Title = d.Title,
                 Date = d.Date.Date + " - " + d.EndDate.Date,
                 Link = d.Link,
             }).Take(3);

A couple of other points:

        if (month < System.DateTime.Now.Month)
        {
            return true;
        }
        else if (month == System.DateTime.Now.Month)
        {
            return true;
        }
        else
        {
            return false;
        }

Is the same as: return month >= System.DateTime.Now;

I should say almost the same, since you are unecessarily calling DateTime.Now twice, and you might end up with separate values. Call DateTime.Now once and place it in a variable to ensure that you are consistently checking the same time.

Talljoe
+2  A: 

What is it that you want to do? According to your text you want to find out whether a given date is today or later, but the code sample compares only the month (which means that June this year is the same as June last year). If you want to compare the date (including year and day), this comparison will do the job for you:

yourDate.Date >= DateTime.Now.Date
Fredrik Mörk
Thanks, it works
Frozzare