C# 在数组中选择备用值
我有这样一个时间段:C# 在数组中选择备用值,c#,C#,我有这样一个时间段: TimeSpan Midnight = new TimeSpan(24, 0, 0); List<DateTime> Timeslot = new List<DateTime>(); Timeslot.Add(BookingStart) Timeslot.Add(BookingEnd) Timeslot.Add(breakstart1) Timeslot.Add(breakEnd1) Timeslot.Add(breakstart2) Timeslo
TimeSpan Midnight = new TimeSpan(24, 0, 0);
List<DateTime> Timeslot = new List<DateTime>();
Timeslot.Add(BookingStart)
Timeslot.Add(BookingEnd)
Timeslot.Add(breakstart1)
Timeslot.Add(breakEnd1)
Timeslot.Add(breakstart2)
Timeslot.Add(breakEnd2)
Timeslot.Add(breakstart3)
Timeslot.Add(breakEnd3)
for (int i = 1; i <= Timeslot.Count - 1; i++)
{
if (Timeslot[0] != Timeslot[1])
{
if ((Timeslot[i].TimeOfDay < Midnight) &&
(dutyEnd.TimeOfDay >= Midnight))
{
BookedHours = Midnight - Timeslot[i].TimeOfDay;
// if i value is one then i want get the value like
// BookedHours = Midnight - Timeslot[i,End].TimeOfDay;
// BookedHours = Midnight - Timeslot[breakEnd1].TimeOfDay;
}
}
}
现在这有意义吗?根本不清楚你想做什么。 这没有多大意义:
if (Timeslot[0] != Timeslot[1]);
但我怀疑您试图做的不是关注列表中的项目本身,而是关注连续的对(Item1与Item2、Item2与Item3、Item3与Item4等)
实现这一点的一种方法是将时间段与自身的偏移版本合并,如下所示:
foreach (var timewindow in Timeslot.Zip(Timeslot.Skip(1),Tuple.Create))
{
Console.WriteLine(String.Format("handling duration from {0} to {1}",timewindow.Item1,timewindow.Item2));
}
我的解决方案
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication11
{
class Program
{
static void Main(string[] args)
{
var bookingStartsAndEnds = new List<DateTime>()
{
DateTime.Parse("1999-02-01 13:50:00"),
DateTime.Parse("1999-02-03 13:50:00"),
DateTime.Parse("1999-02-04 13:05:00"),
DateTime.Parse("1999-02-04 13:15:00"),
};
var bookedHours = bookingStartsAndEnds
// order by the date ascending
.OrderBy(dt => dt)
// select only the "firsts" of the tuples
.Where((dt, i) => i%2 == 0)
// select the first + the next
.Select((dt, i) => Tuple.Create<DateTime, DateTime?>(dt, bookingStartsAndEnds.ElementAtOrDefault((i*2) + 1)))
// filter not-matching-end (the list must contain even number of items only!)
.Where(t => t.Item2 != null)
// calculate the time-difference between end-date and start-date and get all hours
.Select(t => (t.Item2.Value - t.Item1).TotalHours)
// sum them up
.Sum();
Console.WriteLine("{0:0.00} hours dude! this will be expensive...", bookedHours);
Console.Read();
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
命名空间控制台应用程序11
{
班级计划
{
静态void Main(字符串[]参数)
{
var bookingstartsands=新列表()
{
解析(“1999-02-01 13:50:00”),
DateTime.Parse(“1999-02-03 13:50:00”),
解析(“1999-02-04 13:05:00”),
解析(“1999-02-04 13:15:00”),
};
var bookedHours=bookingStartsAndEnds
//按升序日期订购
.OrderBy(dt=>dt)
//仅选择元组的“第一个”
其中((dt,i)=>i%2==0)
//选择第一个+下一个
.Select((dt,i)=>Tuple.Create(dt,bookingstartsands.elementatorderfault((i*2)+1)))
//筛选器不匹配结束(列表必须仅包含偶数个项目!)
.Where(t=>t.Item2!=null)
//计算结束日期和开始日期之间的时间差,并获取所有小时数
.选择(t=>(t.Item2.Value-t.Item1).TotalHours)
//总结一下
.Sum();
WriteLine(“{0:0.00}小时伙计!这会很贵…”,bookedhurs);
Console.Read();
}
}
}
for(int i=1;i
您只想计算给定休息时间或给定预订的预定时间吗?(那么这是一种反转逻辑)。。。它应该是时间段之间/时间段内所有预定时间的总和,对吗?您使用的是哪个.net版本?@Usher您能重新表述您的问题吗?“你想达到什么目的一点也不清楚。”他说,没错。我正在使用.net 4don’我不知道MIDNIGHT告诉我们的逻辑是什么,但我会提供一个答案……zip是highskill优雅;)但是结果列表将包含3项而不是两项,因为只有“左侧”是移位的一项。。。
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication11
{
class Program
{
static void Main(string[] args)
{
var bookingStartsAndEnds = new List<DateTime>()
{
DateTime.Parse("1999-02-01 13:50:00"),
DateTime.Parse("1999-02-03 13:50:00"),
DateTime.Parse("1999-02-04 13:05:00"),
DateTime.Parse("1999-02-04 13:15:00"),
};
var bookedHours = bookingStartsAndEnds
// order by the date ascending
.OrderBy(dt => dt)
// select only the "firsts" of the tuples
.Where((dt, i) => i%2 == 0)
// select the first + the next
.Select((dt, i) => Tuple.Create<DateTime, DateTime?>(dt, bookingStartsAndEnds.ElementAtOrDefault((i*2) + 1)))
// filter not-matching-end (the list must contain even number of items only!)
.Where(t => t.Item2 != null)
// calculate the time-difference between end-date and start-date and get all hours
.Select(t => (t.Item2.Value - t.Item1).TotalHours)
// sum them up
.Sum();
Console.WriteLine("{0:0.00} hours dude! this will be expensive...", bookedHours);
Console.Read();
}
}
}