Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.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# 您能否帮助筛选一个集合并在linq中创建另一个集合_C#_Linq To Objects - Fatal编程技术网

C# 您能否帮助筛选一个集合并在linq中创建另一个集合

C# 您能否帮助筛选一个集合并在linq中创建另一个集合,c#,linq-to-objects,C#,Linq To Objects,我创建了以下内容来解释我的问题 给出了一个包含付款日期和付款期的列表。我需要创建一个新的列表,其中包含最接近其付款日期的每个期间的1项 因此,在这个示例中,列表应返回两个项目“本月1日”和“本月17日”,因为它们在其期间最接近其付款日期 有什么建议吗 private static List<Schedule> GetFilteredScheduleList() { List<Schedule>oldScheduleList=new List<Sche

我创建了以下内容来解释我的问题

给出了一个包含付款日期和付款期的列表。我需要创建一个新的列表,其中包含最接近其付款日期的每个期间的1项

因此,在这个示例中,列表应返回两个项目“本月1日”和“本月17日”,因为它们在其期间最接近其付款日期

有什么建议吗

 private static List<Schedule> GetFilteredScheduleList()
  {
     List<Schedule>oldScheduleList=new List<Schedule>();
     oldScheduleList.Add(new Schedule { PayDate = new DateTime(2011, 1, 1), Id = 1, Name = "1st of the month", Period = SchedulePeriod.Monthly });
     oldScheduleList.Add(new Schedule { PayDate = new DateTime(2011, 1, 4), Id = 1, Name = "4th of the month", Period = SchedulePeriod.Monthly });
     oldScheduleList.Add(new Schedule { PayDate = new DateTime(2011, 1, 19), Id = 1, Name = "19th of the month", Period = SchedulePeriod.Monthly });
     oldScheduleList.Add(new Schedule { PayDate = new DateTime(2012, 1, 3), Id = 1, Name = "3rd of each quarter", Period = SchedulePeriod.Quarterly });
     oldScheduleList.Add(new Schedule { PayDate = new DateTime(2013, 1, 8), Id = 1, Name = "8th each quarter", Period = SchedulePeriod.Quarterly });
     oldScheduleList.Add(new Schedule { PayDate = new DateTime(2011, 1, 17), Id = 1, Name = "17th of the month", Period = SchedulePeriod.Quarterly });

    // Need to create a new list containing 1 item for each period that is nearest to its Pay Date.Starting point it's today's date.
    //  so the list should return 2 items "1st of the month" and "17th of the month" as they are the closest to their paydate in their period

     List<Schedule> newScheduleList = oldScheduleList.Where(x=>x.PayDate)
  }
私有静态列表GetFilteredScheduleList()
{
ListoldScheduleList=新列表();
oldScheduleList.Add(新计划{PayDate=newdatetime(2011,1,1),Id=1,Name=“本月1日”,Period=SchedulePeriod.Monthly});
添加(新计划{PayDate=newdatetime(2011,1,4),Id=1,Name=“本月的第4天”,Period=SchedulePeriod.Monthly});
添加(新计划{PayDate=newdatetime(2011,1,19),Id=1,Name=“本月19日”,Period=SchedulePeriod.Monthly});
oldScheduleList.Add(新计划{PayDate=new DateTime(2012,1,3),Id=1,Name=“每个季度的第三次”,Period=SchedulePeriod.Quarterly});
oldScheduleList.Add(新计划{PayDate=newDateTime(2013,1,8),Id=1,Name=“每季度8次”,期间=SchedulePeriod.Quarterly});
新增(新计划{PayDate=newdatetime(2011,1,17),Id=1,Name=“本月17日”,Period=SchedulePeriod.Quarterly});
//需要为最接近其付款日期的每个期间创建一个包含1项的新列表。起点是今天的日期。
//因此,该列表应返回两个项目“本月1日”和“本月17日”,因为这两个项目最接近其期间的付款日期
List newScheduleList=oldScheduleList.Where(x=>x.PayDate)
}

这应该从每个最接近现在的时段中选择
计划
项。这就是你的意思吗

oldScheduleList
    .GroupBy(s => s.Period)
    .Select(
        g => g
              .OrderBy(s => Math.Abs((s.PayDate - DateTime.Now).TotalSeconds))
              .First()
    )
编辑:

作为对您评论的回应,获取该期间的第一项稍微简单一些。我已经注释了一个
Where
子句。这将获得从现在开始的第一个输入周期(即忽略已过期的日期)


这应该从离现在最近的每个时段中选择
计划
项。这就是你的意思吗

oldScheduleList
    .GroupBy(s => s.Period)
    .Select(
        g => g
              .OrderBy(s => Math.Abs((s.PayDate - DateTime.Now).TotalSeconds))
              .First()
    )
编辑:

作为对您评论的回应,获取该期间的第一项稍微简单一些。我已经注释了一个
Where
子句。这将获得从现在开始的第一个输入周期(即忽略已过期的日期)


谢谢你的回复。这很有效。只是比较一下期限内的日期,然后得到第一个。这就是我真正想要的。很抱歉没有正确解释,谢谢你的帮助。谢谢你!!!如果您有时间,可以在第一个答案中解释math.abs的作用。再次感谢math.abs给出了其单个参数的绝对值。它的逻辑如下:对于一个值x,如果x感谢你的回复。这是有效的。那就比较这段时间内的日期并得到第一个。这才是我真正想要的。很抱歉没有正确解释,谢谢你的帮助。谢谢你!!!如果您有时间,可以在第一个答案中解释math.abs的作用。再次感谢math.abs给出了其单个参数的绝对值。其逻辑如下:对于值x,如果x