现在唯一的冲突是当两个endDates(代码中的10,15)min值对着同一个startDate(25)时。
解决方案:我想在这两分钟内取出最小值。由endDate冲突给出的值。
public class DateController : Controller
public ActionResult date()
{
int allDiff;
List<int> list=new List<int>();
int flag = 0;
DateTime[] startDate = new DateTime[3];
startDate[0] = new DateTime(2011, 11, 5);
startDate[1] = new DateTime(2011, 11, 7);
startDate[2] = new DateTime(2011, 11, 25);
DateTime[] endDate = new DateTime[3];
endDate[0] = new DateTime(2011, 11, 10);
endDate[1] = new DateTime(2011, 11,15);
endDate[2] = new DateTime(2011, 11, 30);
DateTime Min= startDate.Min();
DateTime Max = endDate.Max();
TimeSpan span = Max - Min;
int total = span.Days;
ViewBag.globalTotal = total;
foreach (DateTime e in endDate)
{
foreach (DateTime s in startDate)
{
if (s >= e)
{
TimeSpan span1 = s - e;
allDiff = span1.Days;
list.Add(allDiff);
flag = 1;
}
else {
flag = 0;
}
}
if (flag == 1)
{
int m = list.Min();
ViewBag.dhiraj = m;
total = total - m;
list.Clear();
}
}
ViewBag.Total = total;
return View();
}
答案 0 :(得分:0)
虽然我同意上面提到的问题,但我认为我设法理解你要做的事情,用以下内容替换你的两个for循环:
//Create list of startdates that are greater than any of the end dates
//and return the differences in days
var list = from s in startDate
from e in endDate
where s > e
select (s-e).Days;
//If any were found, take out the min of those
int dhiraj = list.Count() == 0 ? 0 : list.Min();
//Update total
total -= dhiraj;
答案 1 :(得分:0)
工作代码
public ActionResult date()
{
int j = 0;
int output = 0;
DateTime[] startDate = new DateTime[6];
startDate[0] = new DateTime(2011, 11, 1);
startDate[1] = new DateTime(2011, 11, 6);
startDate[2] = new DateTime(2011, 11, 16);
startDate[3] = new DateTime(2011, 11, 17);
startDate[4] = new DateTime(2011, 11, 17);
startDate[5] = new DateTime(2011, 11, 17);
DateTime[] endDate = new DateTime[6];
endDate[0] = new DateTime(2011, 11, 4);
endDate[1] = new DateTime(2011, 11, 8);
endDate[2] = new DateTime(2011, 11, 18);
endDate[3] = new DateTime(2011, 11, 17);
endDate[4] = new DateTime(2011, 11, 22);
endDate[5] = new DateTime(2011, 11, 19);
//5-7
//7-9
//15-20
List<DateTime> start = new List<DateTime>();
List<DateTime> end = new List<DateTime>();
DateTime interStart = default(DateTime);
DateTime interEnd = default(DateTime);//initialize
start.Add(interStart);
end.Add(interEnd);
for (int i = 0; i < startDate.Length ; i++)
{
if(i < (startDate.Length-1)){
j=i+1;
}
else{
j = i;
}
if ((startDate[i] <= endDate[j]) && (endDate[i] >= startDate[j]))
{
List<DateTime> intermediateStart = new List<DateTime>();
intermediateStart.Add(startDate[i]);
intermediateStart.Add(startDate[j]);
interStart = intermediateStart.Min();
List<DateTime> intermediateEnd = new List<DateTime>();
intermediateEnd.Add(endDate[i]);
intermediateEnd.Add(endDate[j]);
interEnd = intermediateEnd.Max();
if ((start.Last() <= interEnd) && (end.Last() >= interStart))
{
List<DateTime> finalStartValue = new List<DateTime>();
finalStartValue.Add(start.Last());
finalStartValue.Add(interStart);
List<DateTime> finalEndValue = new List<DateTime>();
finalEndValue.Add(end.Last());
finalEndValue.Add(interEnd);
DateTime finalStart = finalStartValue.Min();
DateTime finalEnd = finalEndValue.Max();
start.Remove(start.Last());
end.Remove(end.Last());
start.Add(finalStart);
end.Add(finalEnd);
}
else
{
start.Add(interStart);
end.Add(interEnd);
}
}
else
{
start.Add(startDate[i]);
end.Add(endDate[i]);
//remove(start.first);
}
}
int count=(start.Count())-1;
for (int k = 0; k <= count; k++) {
TimeSpan span = end[k]-start[k];
int diff = span.Days;
output = diff + output;
}
ViewBag.finalOutput = output;
return View();
}
}}