I configured Solr index to the path where I created some articles using templates, templates have the same fields with data type as in below model


My model looks like this

public class Article : SearchResultItem
    public string ItemPath { get; set; }

    public string Id { get; set; }

    public string Title { get; set; }

    public DateTime PublishedDate { get; set; }

    public string Summary { get; set; }


When I query I can get the list of articles and I can read correct PublishedDate, but when I extend my query where I want articles in a year

list.Where(c => c.PublishedDate.Year == 2017).ToList();

It returns zero rows

  • I imagine (while sloppy) you could query based on a date range instead, i.e. 01/01/2017 to 12/31/2017. Looking at a previous project, I see we got the entire result set, then filtered by year after-the-fact. But curious about a pure Solr solution.
    – jrap
    May 30, 2019 at 20:34
  • I have to filter by different parameters,like in year, in month, in month of year etc. it will lead to ugly code practice, i was also expecting may be with Sitecore9/Solr this time its improved, or if there is some extension work around
    – sairfan
    May 30, 2019 at 20:42
  • 2
    You can always create computed fields specifically for Year/Month. That will make your query clean.
    – jrap
    May 30, 2019 at 20:44
  • Sounds like a good approach, can you please give me an example how should i configure it in index configurations and do i need to create such field in template as well? thanks
    – sairfan
    May 30, 2019 at 20:56

2 Answers 2


As mentioned by @jrap in the comments, you can use computed fields to solve this.

To use computed field, you would need to

1) Create a class that contains the logic to generate/calculate your new computed field

public class ComputedYearField: IComputedIndexField

 public string FieldName { get; set; }
 public string ReturnType { get; set; }

 public object ComputeFieldValue(IIndexable indexable) {

  Item item = (indexable as SitecoreIndexableItem);

  if (item != null)

   // check if item is an Article
   if(item.TemplateID == "YOURARTICLETEMPLATEID")
    // extract the year from PublishDate field 
    var dateField = (DateField)item.Fields["PublishedDate"];
    var itemDate = Sitecore.DateUtil.IsoDateToDateTime(dateField.Value);

    return itemDate.Year
  return null;


2) Patch your Sitecore Solr(or lucene) settings so that the computed field will be run during indexing.

<fields hint="raw:AddComputedIndexField">
<field fieldName="ComputedYear" returnType="int">

3) Run reindex of your Sitecore indexes. Then you would be able to use your computed field

list.Where(c => c.ComputedYear == 2017).ToList();

You can find more details on computed fields in this blogpost

PS: I have not tested the code above

  • 1
    To wrap it up, the OP would also need: [IndexField("ComputedYear")] public int ComputedYear { get; set; } added to Article.
    – jrap
    May 31, 2019 at 12:45

If you want to query against Date field you should compare with Datetime type.

date.Year, date.Month etc. don`t work with date field queries.

Rewrite your query like this:

var dateStart = new DateTime(year, 1, 1);
var dateEnd = new DateTime(year, 12, DateTime.DaysInMonth(year,12));

list.Where(x => x.Date.Between(dateStart, dateEnd, Inclusion.Both));
// or
// list.Where(x => x.Date>=dateStart && x.Date<=dateEnd);
  • I tried the above but it does not compares with date at all, what could be the reason? for example to made it simple i tried list.Where(x => x.Date < Datetime.Today).ToList() still returns all rows
    – sairfan
    May 31, 2019 at 14:56
  • How your field looks in solr, like: "publisheddate_tdt":"2019-04-11T21:00:00Z" ?
    – x3mxray
    May 31, 2019 at 15:09
  • "publisheddate_tdt":"2019-04-21T06:00:00Z" exactly copied from Solr, and on Sitecore template its data type is Datetime
    – sairfan
    May 31, 2019 at 15:33
  • Hmm.. It should work OOTB if you your field is general field (not computed field). If computed - just be sure that is has returnType="datetime"
    – x3mxray
    Jun 1, 2019 at 22:29
  • By rebuilding index i was able to query by date, BTW i was not able to find Date.Between method on date, am i missing some library to add? thanks
    – sairfan
    Jun 7, 2019 at 17:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.