'Compare datetime column just year (without time-day-month) with Linq

I want to get a 2022 records. But my code is not efficient. How to compare it?

> var _year = "2022"; list= query.Where(x =>
> Convert.ToDateTime(x.created_time).ToString("YYYY").Equals(_year
> )).ToList();

                 


Solution 1:[1]

Is better not to change type to string, but get DateTime structure Year property.

Code example:

  var year = 2022;
  var list = query.Where(x => Convert.ToDateTime(x.created_time).Year.Equals(year)).ToList();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dmitry Grebennikov