Created
July 30, 2012 14:47
-
-
Save hoganlong/3207484 to your computer and use it in GitHub Desktop.
SO Answer test for http://stackoverflow.com/a/11723399/215752
This file contains hidden or 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
void Main() | |
{ | |
List<offer> OfferList = new List<offer>() { | |
new offer() { id=1, f=new DateTime(2011,1,1), t=new DateTime(2011,1,31),status=true}, | |
new offer() { id=1, f=new DateTime(2011,2,1), t=new DateTime(2011,2,28), status=false}, | |
new offer() { id=2, f=new DateTime(2011,2,1), t=new DateTime(2011,2,28), status=false}, | |
new offer() { id=3, f=new DateTime(2011,1,1), t=new DateTime(2011,1,31), status=true }, | |
new offer() { id=3, f=new DateTime(2011,1,1), t=new DateTime(2011,1,26), status=true }, | |
new offer() { id=4, f=new DateTime(2011,1,1), t=new DateTime(2011,1,31), status=true }, | |
new offer() { id=1, f=new DateTime(2011,3,1), t=new DateTime(2011,3,31), status=false}, | |
}; | |
var r = (OfferList.GroupBy(offer => offer.id) | |
.Select(group => | |
new { offerid = group.Key, | |
offers = group.OrderBy(o => o.f), | |
count = group.Count() }) | |
.OrderBy(g => g.offers.First().f) // list with the oldest | |
.ThenByDescending(g => g.count)).Dump() // then by most | |
.First().offers.First(); | |
r.Dump(); | |
} | |
// Define other methods and classes here | |
public class offer | |
{ | |
public int id { get; set; } | |
public DateTime f { get; set; } | |
public DateTime t { get; set; } | |
public bool status { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment