开发者

Using Linq To Get Data By Date

开发者 https://www.devze.com 2023-01-23 19:24 出处:网络
I have a list< item > of the following public class Item { public string Link { get; set; } public string Title { get; set; }

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 开发者_StackOverflow中文版out items only with a PublishDate of today?

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


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


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


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号