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

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

代码如下:

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
*/
相关推荐
我是苏苏10 小时前
C#基础:使用Linq进行简单去重处理(DinstinctBy/反射)
开发语言·c#·linq
亦世凡华、1 天前
掌握.NET Core后端发布流程,如何部署后端应用?
经验分享·.netcore·docker部署·程序发布
2501_903238652 天前
Kafka中commitAsync的使用与实例解析
分布式·kafka·个人开发·linq
contact972 天前
.NET Core中的五种过滤器详解
.netcore·过滤器
以为不会掉头发的詹同学2 天前
【 Avalonia UI 语言国际化 I18n】图文结合教学,保姆级教学,语言国际化就是这么简单(.Net C#)
开发语言·前端·c#·.netcore·用户界面
爱吃香蕉的阿豪4 天前
在c#中虚方法和抽象类的区别
深度学习·c#·.netcore
shepherd枸杞泡茶4 天前
第3章 .NETCore核心基础组件:3.1 .NET Core依赖注入
开发语言·c#·.net·.netcore
.NET快速开发框架4 天前
使用nvm管理node.js版本,方便vue2,vue3开发
vue·.netcore·常用工具·开发技术·rdif
白露与泡影5 天前
Kafka 为什么会丢消息?如何保证消息不丢失?
分布式·kafka·linq
csdn_aspnet6 天前
ASP.NET Core 使用 FileStream 将 FileResult 文件发送到浏览器后删除该文件
asp.net·.netcore