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

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

代码如下:

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
*/
相关推荐
.BOM1 天前
.Net中WebApiController如何实现多版本兼容?
.netcore
2501_941807261 天前
Java高性能消息队列与Kafka实战分享:大规模消息处理、异步通信与性能优化经验
c#·linq
鹿衔`1 天前
通过Flink 1.19 客户端实现Flink集群连接 Kafka 基础测试报告
c#·linq
E***U9451 天前
C在物联网中的.NET Core
物联网·.netcore
20岁30年经验的码农2 天前
Kafka 消息中间件实战指南
分布式·kafka·linq
勿芮介3 天前
[开发技术]在.NetCore中使用Elsa工作流-初学篇
.netcore
我是苏苏3 天前
已解决:.NetCore控制台程序(WebAPI)闪退,没有报错日志定位不到信息
.netcore
时光追逐者3 天前
C#/.NET/.NET Core技术前沿周刊 | 第 62 期(2025年11.17-11.23)
c#·.net·.netcore
宝桥南山3 天前
.NET 10 - Blazor web assembly应用的一些诊断方式
microsoft·微软·c#·asp.net·.net·.netcore