Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Linq To Get Data By Date

Tags:

c#

datetime

linq

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?

like image 509
YodasMyDad Avatar asked Dec 13 '22 18:12

YodasMyDad


2 Answers

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
like image 53
Dennis Burton Avatar answered Dec 15 '22 06:12

Dennis Burton


DateTime today = DateTime.Today;
var todayItems = list.Where(item => item.PublishDate.Date == today);
like image 36
Albin Sunnanbo Avatar answered Dec 15 '22 07:12

Albin Sunnanbo