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

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

代码如下:

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
*/
相关推荐
武藤一雄1 小时前
C#:nameof 运算符全指南
开发语言·microsoft·c#·.net·.netcore
武藤一雄5 小时前
C# 核心技术解析:Parse vs TryParse 实战指南
开发语言·windows·microsoft·微软·c#·.netcore
武藤一雄7 小时前
深入理解 C# 中的 sizeof 与非托管类型约束
开发语言·windows·c#·.net·.netcore
武藤一雄8 小时前
C# 中精准锁定类型信息指南:typeof vs GetType()
开发语言·windows·c#·.net·.netcore
csdn_aspnet2 天前
ASP.NET Core:使用 JavaScript 加密并在控制器中解密
javascript·asp.net·.netcore
武藤一雄3 天前
从零构建C# OOP 知识体系
windows·microsoft·c#·.net·.netcore·oop
半桶水专家3 天前
Kafka 4.0.1 KRaft 模式完整部署指南
分布式·kafka·linq
大大大大晴天️3 天前
Flink技术实践-实时场景技术方案选型
大数据·flink·linq
heimeiyingwang5 天前
【架构实战】消息队列 Kafka 架构分析
架构·kafka·linq
sjmaysee6 天前
Spring Boot集成Kafka:最佳实践与详细指南
spring boot·kafka·linq