Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 检查特定天数是否存在于给定周的设定范围内,并添加到列表中_C#_List_Date_Days - Fatal编程技术网

C# 检查特定天数是否存在于给定周的设定范围内,并添加到列表中

C# 检查特定天数是否存在于给定周的设定范围内,并添加到列表中,c#,list,date,days,C#,List,Date,Days,在获取和返回3周范围内的特定日期列表时,最佳选择是什么 我的目的是根据配送中心的可用天数创建配送日期 public List DeliveryDays{get;set;} DeliveryDays包含0-6之间的设置值(0表示星期日,1表示星期一等) 我希望获得这些值,将它们传递给3周的后续日期,并在列表中返回这些交货日期(因此只有这些中心可以在特定日期订购) 以下是我目前掌握的情况: public List<DateTime> CalculateAvailableDeliveryD

在获取和返回3周范围内的特定日期列表时,最佳选择是什么

我的目的是根据配送中心的可用天数创建配送日期

public List DeliveryDays{get;set;}

DeliveryDays包含0-6之间的设置值(0表示星期日,1表示星期一等)

我希望获得这些值,将它们传递给3周的后续日期,并在列表中返回这些交货日期(因此只有这些中心可以在特定日期订购)

以下是我目前掌握的情况:

public List<DateTime> CalculateAvailableDeliveryDates()
        {

            //Loop through 3 weeks of days
            DateTime today = DateTime.Today;                                                            //Specify today's date
            DateTime totalDateCount = today.AddDays(1);                                                 //Plus one day on each time counted

            var dates = Enumerable.Range(0, 21).Select(days => totalDateCount.AddDays(days)).ToList();  //Count from 0 to 21 (3 weeks worth of days). On each count, run totalDateCount

            //if exists in deliveryDayList
                //add to dates via dates.Add func

            if (DeliveryDays.Contains(DayOfWeek.Monday))
            {
                //action
            } //and so on for each day

            //return specific dates from date range
            return dates;
        }
公共列表CalculateAvailableDeliveryDates()
{
//循环3周的工作日
DateTime today=DateTime.today;//指定今天的日期
DateTime totalDateCount=today.AddDays(1);//每次计数加上一天
var dates=Enumerable.Range(0,21)。选择(天=>totalDateCount.AddDays(天)).ToList();//从0到21(相当于3周的天)进行计数。每次计数时,运行totalDateCount
//如果存在于deliveryDayList中
//通过日期添加到日期。添加函数
if(DeliveryDays.Contains(DayOfWeek.Monday))
{
//行动
}//每天如此
//从日期范围返回特定日期
返回日期;
}
目前我得到了21天的读数。if语句不起任何作用,只是作为我的逻辑示例

最好的方法是:不先获取列表,而是根据每个中心的交付日期进行检查并嵌套if/case语句,然后将它们返回到列表中


提前感谢。

给定一个
DayOfWeek
列表,您可以使用
System.Linq
选择下一个
21
天中与一周中某一天匹配的所有日期。
Enumerable.Range
选择一个数字范围,
Select
然后将选择一组表示
今天
加上一些天数的
日期时间
对象,其中
用于过滤结果,比较每个日期的
DayOfWeek
,查看它是否存在于
DeliveryDays

List<DayOfWeek> DeliveryDays = new List<DayOfWeek>();

public List<DateTime> GetAvailableDeliveryDates()
{
    // 1. Get a range of numbers representing the days to add
    //      to today, which will make up our range of dates
    // 2. Select a date using Today.AddDays for each number 
    // 3. Filter on only days which are contained in DeliveryDays

    return Enumerable.Range(0, 21)  // Define the range           
        .Select(i => DateTime.Today.AddDays(i))  // Select the range
        .Where(date => DeliveryDays.Contains(date.DayOfWeek))  // Filter the range
        .ToList();
}
List DeliveryDays=new List();
公共列表GetAvailableDeliveryDates()
{
//1.获取表示要添加的天数的数字范围
//到今天,这将构成我们的日期范围
//2.使用Today.AddDays为每个数字选择一个日期
//3.仅筛选DeliveryDays中包含的天数
return Enumerable.Range(0,21)//定义范围
.Select(i=>DateTime.Today.AddDays(i))//选择范围
.Where(date=>DeliveryDays.Contains(date.DayOfWeek))//筛选范围
.ToList();
}

给定一个
DayOfWeek
列表,您可以使用
System.Linq
选择下一个
21
天中与一周中某一天匹配的所有日期。
Enumerable.Range
选择一个数字范围,
Select
然后将选择一组表示
今天
加上一些天数的
日期时间
对象,其中
用于过滤结果,比较每个日期的
DayOfWeek
,查看它是否存在于
DeliveryDays

List<DayOfWeek> DeliveryDays = new List<DayOfWeek>();

public List<DateTime> GetAvailableDeliveryDates()
{
    // 1. Get a range of numbers representing the days to add
    //      to today, which will make up our range of dates
    // 2. Select a date using Today.AddDays for each number 
    // 3. Filter on only days which are contained in DeliveryDays

    return Enumerable.Range(0, 21)  // Define the range           
        .Select(i => DateTime.Today.AddDays(i))  // Select the range
        .Where(date => DeliveryDays.Contains(date.DayOfWeek))  // Filter the range
        .ToList();
}
List DeliveryDays=new List();
公共列表GetAvailableDeliveryDates()
{
//1.获取表示要添加的天数的数字范围
//到今天,这将构成我们的日期范围
//2.使用Today.AddDays为每个数字选择一个日期
//3.仅筛选DeliveryDays中包含的天数
return Enumerable.Range(0,21)//定义范围
.Select(i=>DateTime.Today.AddDays(i))//选择范围
.Where(date=>DeliveryDays.Contains(date.DayOfWeek))//筛选范围
.ToList();
}

谢谢您的详细介绍!我仍然对范围内的日期时间概念很陌生。非常感谢。谢谢你的帮助!我仍然对范围内的日期时间概念很陌生。非常感谢。