我想創建一個linq查詢,將產生一個日期範圍的集合與容量值的總和考慮到範圍可以重疊的帳戶,我想要一個總和和該重疊期間的不同日期範圍。謝謝。LINQ查詢日期範圍內的總和值
public ActionResult Index()
{
List<Capacities> _list = new List<Capacities>{
new Capacities {StartDate = DateTime.Parse("01/01/2013"), StopDate = DateTime.Parse("01/01/2013 06:00"), Capacity = 100},
new Capacities {StartDate = DateTime.Parse("01/01/2013 04:00"), StopDate = DateTime.Parse("01/02/2013 00:00"), Capacity = 120},
new Capacities {StartDate = DateTime.Parse("01/04/2013"), StopDate = DateTime.Parse("01/04/2013 15:00"), Capacity = 100},
new Capacities {StartDate = DateTime.Parse("01/04/2013 15:00"), StopDate = DateTime.Parse("01/04/2013 18:00"), Capacity = 150}
};
//results expected
//01/01/2013 00:00 - 01/01/2013 04:00 100
//01/01/2013 04:00 - 01/01/2013 06:00 220
//01/01/2013 06:00 - 01/02/2013 00:00 120
//01/04/2013 00:00 - 01/04/2013 15:00 100
//01/04/2013 15:00 - 01/04/2013 18:00 150
return View();
}
public class Capacities
{
public DateTime StartDate { get; set; }
public DateTime StopDate { get; set; }
public int Capacity {get;set;}
}
你有什麼嘗試?你有循環解決方案,並希望將其轉換爲LINQ?你會接受沒有LINQ的解決方案嗎? – 2013-03-23 11:34:58
希望能夠替換一些將遍歷集合的舊代碼並拆分重疊範圍,然後遍歷它來總結創建新數組的容量。是的,我正在尋找一個linq查詢。謝謝。 – user177194 2013-03-23 11:41:04
我想我可以想出一些LINQ,但它不會很漂亮。前一種解決方案可能更易於理解。 – 2013-03-23 11:55:06