简体   繁体   English

Linq datetime日期匹配查询c#

[英]Linq datetime date match in query c#

I am trying to match Datetime.Now's only year(as instance Today is 2014).However i could not do inside of linq query by using datetime.now's year. 我正在尝试匹配Datetime.Now的唯一年份(例如今天是2014年)。但是我无法使用datetime.now的年份在linq查询中进行操作。

LevyServiceHelper.GetAllLevyFee().
List.Where(s=>s.ValidDate==Datetime Today Year).ToList();

How can i match Datetime Today Year with s.ValidDate ? 如何将Datetime Today Year与s.ValidDate匹配?

Thanks. 谢谢。

How about 怎么样

LevyServiceHelper.GetAllLevyFee()

int yr = DateTime.Now.Year;
List.Where(s => s.ValidDate.Year == yr).ToList();

I hope s.ValidDate is DateTime . 我希望s.ValidDateDateTime

Ned's answer will work for 99.99% of the times. 内德的答案将在99.99%的时间内工作。 But will fail when while executing query, year changes where as I have taken year in the beginning meaning all validation will happen on same criteria. 但是当执行查询时,年份会发生变化,就像我一开始就选择年份一样,这意味着所有验证都将在相同条件下进行。

LevyServiceHelper.GetAllLevyFee().
List.Where(s=>s.ValidDate.Year == DateTime.Today.Year).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM