已更新
我在数据库中有一个行程列表,其中包含不同的开始日期和结束日期,我想计算出每次行程与定义的范围相对应的总天数。
例如:
我想查询前往波士顿的天数,范围在以下范围内:
RangeEnd为27.2017年11月 RangeStart为1. 2017年11月
这是我更新的代码:
var travel = new List<Travel>
{
new Travel("Egypt", new DateTime(2017, 8, 4), new DateTime(2017, 8, 24) ),
new Travel("Spain", new DateTime(2017, 11, 1), new DateTime(2017, 12, 10) ),
new Travel("Detroit", new DateTime(2017,9, 15), new DateTime(2017, 12, 20) ),
new Travel("Boston", new DateTime(2017,10, 15), new DateTime(2017, 11, 20) ),
};
var rangeStart = new DateTime(2017, 11, 1);
var rangeEnd = new DateTime(2017, 11, 27);
var Trip = travel.Where(t => t.Country=="Boston");
var sumOfDays= Trip.Sum(t => ????);
此案例的预期结果是20天。 有人可以帮忙吗?
答案 0 :(得分:0)
谢谢@ Flydog57帮助我弄清楚了。我想到了调用另一个函数的想法,并且该函数可以与范围配合使用。
var travel = new List<Travel>
{
new Travel("Egypt", new DateTime(2017, 8, 4), new DateTime(2017, 8, 24) ),
new Travel("Spain", new DateTime(2017, 11, 1), new DateTime(2017, 12, 10) ),
new Travel("Detroit", new DateTime(2017,9, 15), new DateTime(2017, 12, 20) ),
new Travel("Boston", new DateTime(2017,10, 15), new DateTime(2017, 11, 20) ),
};
var rangeStart = new DateTime(2017, 11, 1);
var rangeEnd = new DateTime(2017, 11, 27);
var Trip= travel.Where(s => s != null && s.City == "Boston");
var sumOfDays= Trip.Sum(t => GetTravelDaysWithinRange(t.Start,t.End,rangeStart, rangeEnd));
计算日期的函数是:
public int GetTravelDaysWithinRange( DateTime start, DateTime end, DateTime rangeStart, DateTime rangeEnd)
{
int days = 0;
while (start <= end)
{
if (start.Month>= rangeStart.Month && start.Month <= rangeEnd.Month && start.Year >= rangeStart.Year && start.Year <= rangeEnd.Year)
{
++days;
}
start = start.AddDays(1);
}
return days;
}
得出的精确值为20:))))))
答案 1 :(得分:0)
这可以在1行中完成
var Trip = travel.Where(t => t.Country == "Boston").Select(x => (x.EndDate - x.StartDate).TotalDays).FirstOrDefault();
如果结果为0或撤回次数超过1,则需要先设置或默认设置
答案 2 :(得分:-1)
这样的事情怎么样。我得到的总数是55,这是我期望的:
集体旅行:
public class Travel
{
public Travel(string where, DateTime start, DateTime end)
{
Where = where;
StartDate = start;
EndDate = end;
}
public string Where { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
}
然后食用它:
var travel = new List<Travel>
{
new Travel("Egypt", new DateTime(2017, 8, 4), new DateTime(2017, 8, 24) ),
new Travel("Spain", new DateTime(2017, 9, 1), new DateTime(2017, 10, 1) ),
new Travel("Detroit", new DateTime(2017,9, 15), new DateTime(2017, 9, 20) ),
};
var sumOfTravelDays = travel.Sum(t => (t.EndDate - t.StartDate).Days);
您遇到什么样的问题?
将此功能添加到Travel类(在类中或作为扩展方法):
public int GetTravelDaysWithinRange(DateTime rangeStart, DateTime rangeEnd)
{
//if everything is outside of the range to be considered, return 0
if (rangeEnd < StartDate || rangeStart > EndDate)
{
return 0;
}
//Find the Max of StartDate and rangeStart
var startInRange = (rangeStart < StartDate) ? StartDate : rangeStart;
//And the Min of EndDate and rangeEnd
var endInRange = (rangeEnd > EndDate) ? EndDate : rangeEnd;
//and get the number of days in-between (and then add 1 so Mon-Fri is 5 days, not 4)
return (endInRange - startInRange).Days + 1;
}
现在,当我运行原始代码的变体时,它可以满足我的要求:
var travel = new List<Travel>
{
new Travel("Egypt", new DateTime(2017, 8, 4), new DateTime(2017, 8, 24) ),
new Travel("Spain", new DateTime(2017, 11, 1), new DateTime(2017, 12, 10) ),
new Travel("Detroit", new DateTime(2017,9, 15), new DateTime(2017, 12, 20) ),
new Travel("Boston", new DateTime(2017,10, 15), new DateTime(2017, 11, 20) ),
};
var rangeStart = new DateTime(2017, 11, 1);
var rangeEnd = new DateTime(2017, 11, 27);
var sumOfDates = travel.Sum(t => t.GetTravelDaysWithinRange(rangeStart, rangeEnd));