模拟一个集合 里面是设备号和每日的日期

问题: 需要模拟一个集合 里面是设备号和每日的日期

代码如下:

csharp 复制代码
        static void Main(string[] args)
        {
            string equipmentCodePar = "";

            DateTime time = DateTime.Now; // 获取当前时间
            DateTime startDate = time.AddDays(1 - time.Day);//获取当前月第一天
            DateTime endDate = startDate.AddMonths(1).AddDays(-1); // 例:2023年1月31日

            List<string> Sclist = new List<string>() { "S01", "S02", "S03", "S04", "S05", "S06", "S07", "S08", "S09" };
            if (!string.IsNullOrEmpty(equipmentCodePar)) Sclist = Sclist.Where(p => p.Contains(equipmentCodePar)).ToList();
            //
            var equipmentDateCombinations =
              from equipmentCode in Sclist.Select(info => info)
              from date in Enumerable.Range(0, (endDate - startDate).Days + 1).Select(offset => startDate.AddDays(offset)) 
              select new { EquipmentCode = equipmentCode, Date = date.Date };

            Console.WriteLine("day"+(endDate - startDate).Days);

            foreach (var i in Enumerable.Range(0, (endDate - startDate).Days + 1))
            {

                Console.WriteLine(i);

            }

            Console.WriteLine();
        }

主要使用了

Enumerable.Range(0, (endDate - startDate).Days + 1).Select(offset => startDate.AddDays(offset))

在微软的官方文档中 Enumerable.Range

csharp 复制代码
// Generate a sequence of integers from 1 to 10
// and then select their squares.
IEnumerable<int> squares = Enumerable.Range(1, 10).Select(x => x * x);

foreach (int num in squares)
{
    Console.WriteLine(num);
}

/*
 This code produces the following output:

 1
 4
 9
 16
 25
 36
 49
 64
 81
 100
*/
相关推荐
geovindu1 天前
CSharp: Wordcloud
开发语言·后端·c#·.net·.netcore·词云
geovindu1 天前
CSharp: Command Pattern
开发语言·后端·c#·.net·.netcore·命令模式·行为模式
2601_962064707 天前
SpringBoot 整合 Avro 与 Kafka
spring boot·kafka·linq
datalover8 天前
【无标题】
c#·linq
全栈小59 天前
【C#】.net core,静态方法线程安全解析,面试时经常被问的线程安全就藏在里面
安全·c#·.netcore
清风与日月11 天前
Yitter.IdGenerator:高性能分布式唯一ID生成器详解
分布式·c#·.net·.netcore
骇客野人12 天前
SpringBoot 接入 Kafka 完整实操步骤
spring boot·kafka·linq
Sayai12 天前
Kafka 集群开启 SASL 认证实战(三):外置 ZooKeeper 完整版,quorum 认证 + ACL 访问控制
zookeeper·kafka·linq
海兰12 天前
【Kafka进阶6】顺序消费深度解析
分布式·kafka·linq
Lost of 程序猿14 天前
ASP.NET Core 后台任务全景:从 BackgroundService 到 Channel 队列,再到分布式调度
后端·asp.net·.netcore