Skip to content

Instantly share code, notes, and snippets.

@yicone
Last active December 10, 2015 22:08
Show Gist options
  • Select an option

  • Save yicone/4499715 to your computer and use it in GitHub Desktop.

Select an option

Save yicone/4499715 to your computer and use it in GitHub Desktop.
DateRange and IntRange class. DateRange sample contains group "PriceRow" by date!
void Main()
{
List<PriceRow> prs = new List<PriceRow>{
new PriceRow{
BeginDate = DateTime.Now.Date,
EndDate = DateTime.Now.Date.AddDays(5),
Price = 5000,
},
new PriceRow{
BeginDate = DateTime.Now.Date.AddDays(1),
EndDate = DateTime.Now.Date.AddDays(3),
Price = 3000,
},
};
var q = from pr in prs
from date in new DateRange(pr.BeginDate, pr.EndDate)
group pr by date into g
select new {
Date = g.Key,
Price = g.Min(pr => pr.Price),
};
foreach(var d in q)
{
Console.WriteLine(d);
}
}
public class PriceRow
{
public DateTime BeginDate {get;set;}
public DateTime EndDate {get;set;}
public int Price {get;set;}
}
// Define other methods and classes here
public class DateRange : IEnumerable<DateTime>
{
private DateTime _beginDate;
private DateTime _endDate;
public DateRange(DateTime beginDate, DateTime endDate)
{
_beginDate = beginDate;
_endDate = endDate;
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
public IEnumerator<DateTime> GetEnumerator()
{
for (DateTime date = this._beginDate; date <= this._endDate; date = date.AddDays(1))
{
yield return date;
}
}
}
/// <summary>
/// Range
/// http://stackoverflow.com/questions/8948205/doing-a-range-lookup-in-c-sharp-how-to-implement/8949322#8949322
/// </summary>
/// <typeparam name="TValue"></typeparam>
public class IntRange : IEnumerable<int>
{
public IntRange(int min, int max)
{
this.Min = min;
this.Max = max;
}
public int Max { get; set; }
public int Min { get; set; }
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
public IEnumerator<int> GetEnumerator()
{
for (int i = this.Min; i <= this.Max; i++)
{
yield return i;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment