我有以下類:檢查的日期範圍的日期範圍內
public class Membership
{
public DateTime StartDate { get; set; }
public DateTime? EndDate { get; set; } // If null then it lasts forever
}
我需要確保添加到下面的列表,新項目不重疊,從現有項目的日期時, :
var membership = new List<Membership>
{
new Membership { StartDate = DateTime.UtcNow.AddDays(-10), EndDate = DateTime.UtcNow.AddDays(-5) },
new Membership { StartDate = DateTime.UtcNow.AddDays(-5), EndDate = null }
};
例如做:
var newItem = new Membership { StartDate = DateTime.UtcNow.AddDays(-15), EndDate = DateTime.UtcNow.AddDays(-10) }; // Allowed
var newItem2 = new Membership { StartDate = DateTime.UtcNow.AddDays(-15), EndDate = null }; // Not Allowed
if (AllowededToAdd(newItem))
membership.Add(newItem);
if (AllowededToAdd(newItem2))
membership.Add(newItem2);
我想這將是簡單的,但到目前爲止,我的嘗試已經全部WRO恩,我開始迷惑自己,並希望有人做了類似的事情,他們可以分享。由於
感謝大家的回覆,但我更喜歡這一個,因爲它是最容易理解的哈哈。 – nfplee 2010-09-24 17:53:54