YodasMyDad
YodasMyDad

Reputation: 9485

Using Linq To Get Data By Date

I have a list< item > of the following

public class Item
{
    public string Link { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }
    public DateTime PublishDate { get; set; }
    public FeedType FeedType { get; set; }

    public Item()
    {
        Link = "";
        Title = "";
        Content = "";
        PublishDate = DateTime.Today;
        FeedType = FeedType.RSS;
    }
}

Which is just a parsed RSS feed, I now want to be able to query the List< item > to pull out items only with a PublishDate of today?

However I'm getting a bit lost... Can anyone shed any light please?

Upvotes: 4

Views: 6618

Answers (3)

Preet Sangha
Preet Sangha

Reputation: 65555

List<Item> itemList = ...;
ListItem<Item> filtered items 
                 = itemList.Where(it => it.PublishDate >= DateTime.Today).ToList()

Upvotes: 0

Dennis Burton
Dennis Burton

Reputation: 3333

If I understand correctly the goal here is to strip off the time when comparing.

Extention Method syntax

var today = DateTime.Today;
items.Where( item => item.PublishDate.Date == today );

Query syntax

var today = DateTime.Today;
from item in items
where item.PublishDate.Date == Today
select item

Upvotes: 9

Albin Sunnanbo
Albin Sunnanbo

Reputation: 47068

DateTime today = DateTime.Today;
var todayItems = list.Where(item => item.PublishDate.Date == today);

Upvotes: 4

Related Questions