12

Don't think this is a repost, difficult to search for the word between because it is used in everything (like searching for AND).

I want to filter a list based on a date range.

I have a list with some dates and I want to filter them by a date range. Is there a Linq or Lambda equivalent of the between statement in SQL.

For example, the code below will not work in Linqpad (or Visual Studio):

void Main()
{
    List<ListExample> list = new List<ListExample>();

    list.Add(new ListExample("Name1","23 Aug 2010"));
    list.Add(new ListExample("Name2","23 Aug 2009"));

    var query = from l in list
        where l.DateValue between "01 Jan 2010" and "01 Jan 2011"
        select l;

}

public class ListExample
{

    public ListExample(string name, string dateValue)
    {
        Name = name;
        DateValue = DateTime.Parse(dateValue);
    }

    public string Name{get;set;}
    public DateTime DateValue{get;set;}
}
Mark Hurd
  • 10,665
  • 10
  • 68
  • 101
Simon
  • 196
  • 1
  • 2
  • 10

3 Answers3

28

Something like this?

var query = from l in list
            where l.DateValue >= new DateTime(2010, 1, 1) 
               && l.DateValue <= new DateTime(2011, 1, 1)
            select l;

You can write your own extension method:

public static bool IsBetween(this DateTime dt, DateTime start, DateTime end)
{
   return dt >= start && dt <= end;    
}

In which case the query would look something like (method syntax for a change):

var start = new DateTime(2010, 1, 1);
var end = new DateTime(2011, 1, 1);
var query = list.Where(l => l.DateValue.IsBetween(start, end));

I see you've provided some samples with the dates as strings. I would definitely keep the parsing logic (DateTime.ParseExactor other) separate from the query, if at all possible.

Ani
  • 111,048
  • 26
  • 262
  • 307
  • Thanks I was having problems with the >= and needing to create a DateTime. I find Linq a little confusing because they changed the syntax. – Simon Nov 03 '10 at 13:17
  • @Simon: Cheers. The `>=` doesn't have much to do with LINQ; it's an overloaded operator on `DateTime.` You can use `DateTime.CompareTo` or similar if you prefer. – Ani Nov 03 '10 at 13:19
  • Thanks Ani - the DateTime.Parse was just for my example in LinqPad as I did not want to post a clients code onto the internet. – Simon Nov 03 '10 at 13:24
  • the only thing i've done is to change the extention function to stattic for global usage – Tzvi Gregory Kaidanov Jul 04 '12 at 10:30
  • How would you rewrite your extension method to support Linq to Entities? – Renato Heeb Jun 10 '16 at 09:08
1
var query = from l in list
        where new DateTime(1,1,2010) <= l.DateValue and DateValue <= new DateTime(1,1,2011)
        select l;

of course, normally warning about timezones and different times on clients and servers apply

kͩeͣmͮpͥ ͩ
  • 7,783
  • 26
  • 40
  • Looks like the extension methods are not supported by Linq to Entities if I link to the entity framework. Does this sound correct or am I missing something. – Simon Nov 03 '10 at 15:18
  • 1
    NotSupportedException: Method 'Boolean IsBetween(System.Nullable`1[System.DateTime], System.DateTime, System.DateTime, Boolean)' has no supported translation to SQL. – Simon Nov 03 '10 at 15:43
1
Datetime DT1 = DateTime.Parse("01 Jan 2010");
Datetime DT2 = DateTime.Parse("01 Jan 2011");
var query = from l in list
            where l.DateValue >= DT1 && l.DateValue <= DT2
            select l;

in linq you use the && and || like you would in a normal boolean statement of C#.

Stefanvds
  • 5,868
  • 5
  • 48
  • 72