Last active
October 10, 2016 21:13
-
-
Save dialedin2014/d9eb92bf1b021da9bb500948bb4a5e7b to your computer and use it in GitHub Desktop.
GetFreshestBeers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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