ravendb/ravendb — Issue #461: Cannot use Max with DateTime / long in the reduce part of an index.
Author: kroffdaddy
Created: 2012-02-24 08:09:32+00:00
State: open

I'm trying to create a map/reduce index that uses `IGrouping<,>.Max` to get the maximum date in the reduction part of my index. Here is a small piece of code to demonstrate the problem:

public class Post
{
public string PostId { get; set; }
public DateTime PostedAt { get; set; }
}

public class LastPostIndex : AbstractIndexCreationTask
{
public class ReduceResult
{
public string Id { get; set; }
public DateTime Posted { get; set; }
}

public LastPostIndex()
{
Map = docs =>
from doc in docs
select new { Id = doc.PostId, Posted = doc.PostedAt };

Reduce = maps =>
from map in maps
group map by map.Id
into g
select new { Id = g.Key, Posted = g.Max(x => x.Posted) };
}
}

This will give me the following index error (one per Post document) in Raven Studio:

> Cannot implicitly convert type 'System.DateTime' to 'int'

The problem lies in `Posted = g.Max(x => x.Posted)`. If I convert the `DateTime` object in the map part to a `long` I still get a conversion error. Compressing it to an `int` works fine though, which makes me suspect that **you lack support for DateTime and long in this scenario**.