LINQ in Action - LINQ Book & News

Calculating baseball statistics with Linq

If you are fed up with the obligatory business use cases used in Linq samples - you know, the Customers and Orders guys from the Northwind database - you may be interested in seeing less boring queries.
Derek Slager has published some Linq queries he wrote to calculate baseball stats.

var battingAverageLeaders =
  from game in games
  from play in game.GetPlays()
  where play.Result != null
  group play by play.Batter into g
  where g.Count(p => p.Result.IsAtBat) > 400
  let pd = new PlayerData {
    Player = g.Key,
    Data = (Math.Round(1000.0d *
                ((double)g.Count(p => p.Result.IsHit) /
                 (double)g.Count(p => p.Result.IsAtBat))) / 1000.0d).ToString(".000") }
  orderby pd.Data descending
  select pd;
Published Thursday, October 12, 2006 4:13 PM by Fabrice Marguerie



Linq in Action News said:

Do you want to see LINQ used for something else than querying a database for customers or for querying

March 28, 2007 2:16 PM
New Comments to this post are disabled