Hello,
I'd like to display an average of the last 20 series played by the bowlers on my team.
That is, for each one of them show their total average and also their average for the last
20 series. Total average is easy with map/reduce, but average for last 20 does not seem
to fit with this model. I can calculate the last 20 for a
single player like follows:
public class Pins_ByMatchDate : AbstractIndexCreationTask<Match, Pins_ByMatchDate.Result>
{
public Pins_ByMatchDate()
{
Map = matches => from match in matches
from team in match.Teams
from serie in team.Series
from table in serie.Tables
from game in table.Games
select new
{
Date = match.Date.ToShortDateString(),
Player = game.Player,
Pins = game.Pins
};
Store(x => x.Date, FieldStorage.Yes);
Store(x => x.Player, FieldStorage.Yes);
Store(x => x.Pins, FieldStorage.Yes);
}
public class Result
{
public string Date { get; set; }
public string Player { get; set; }
public int Pins { get; set; }
}
}
Then:
public ActionResult Last(int count, string player)
{
var list = Session.Query<Pins_ByMatchDate.Result, Pins_ByMatchDate>()
.Where(r => r.Player == player)
.OrderByDescending(r => r.Date)
.AsProjection<Pins_ByMatchDate.Result>()
.Take(count)
.ToList();
return Json(list, JsonRequestBehavior.AllowGet);
}
And
http://localhost:63818/Home/Last?count=2&player=Kjell%20Persson returns
[{"Date":"03/26/2011","Player":"Kjell Persson","Pins":172},{"Date":"03/26/2011","Player":"Kjell Persson","Pins":194}]
But how can I create an index with
all players, last-n average precalculated? I.e. I want
http://localhost:63818/Home/Last?count=2to return
[{"Player":"Kjell Persson","Pins":368,"Games":2},{"Player":"Mikael Axelsson","Pins":388,"Games":2}]
For this example I used 2. I want 20 but I can just as well hard-code it in the index, if necessary. I.e. I don't need to have a dynamic value.
I'd be thankful for any suggestions!
/Daniel Lidström
Stockholm, Sweden