Skip to content

Instantly share code, notes, and snippets.

@dialedin2014
Last active October 10, 2016 21:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dialedin2014/d9eb92bf1b021da9bb500948bb4a5e7b to your computer and use it in GitHub Desktop.
Save dialedin2014/d9eb92bf1b021da9bb500948bb4a5e7b to your computer and use it in GitHub Desktop.
GetFreshestBeers
private class Bottle
{
public int ID { get; set; }
public DateTime BottledDate { get; set; }
}
[TestMethod()]
public void JoinOnMaxDate()
{
List<int> bottleNumbers = new List<int>(6) { 1, 2, 3, 4, 5, 6 };
List<Bottle> bottles = new List<Bottle>(7) {
new Bottle {ID =1,BottledDate=DateTime.Now.AddDays(-7) },
new Bottle {ID =1,BottledDate=DateTime.Now.AddDays(-6) },
new Bottle {ID =2,BottledDate=DateTime.Now.AddDays(-7) },
new Bottle {ID =3,BottledDate=DateTime.Now.AddDays(-7) },
new Bottle {ID =4,BottledDate=DateTime.Now.AddDays(-7) },
new Bottle {ID =5,BottledDate=DateTime.Now.AddDays(-7) },
new Bottle {ID =6,BottledDate=DateTime.Now.AddDays(-7) }
};
var fresherBottles = bottles.Join(
bottleNumbers,
b => b.ID,
bn => bn,
(b2, r) => b2)
.OrderBy(x => x.BottledDate).GroupBy(x => x.ID)
.Select(g => g.First())
.ToList();
fresherBottles.ForEach(b => Debug.WriteLine(
string.Format("Bottle {0}, bottled on {1}", b.ID, b.BottledDate.ToLocalTime())));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment