Skip to content

Instantly share code, notes, and snippets.

@hoganlong
Created July 30, 2012 14:47
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 hoganlong/3207484 to your computer and use it in GitHub Desktop.
Save hoganlong/3207484 to your computer and use it in GitHub Desktop.
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