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

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

代码如下:

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
*/
相关推荐
liudashuang201712 小时前
Kafka Producer 隐藏深坑:Sender 线程自阻塞(自死锁)导致 BufferExhaustedException 完整复盘
java·分布式·kafka·linq
宝桥南山1 天前
Microsoft Agent Framework(.NET) - 尝试一下从MCP Server上获取Agent Skills
ai·微软·c#·aigc·.net·.netcore
御坂嘀喵 白日焰火8 天前
LINQ之路18:LINQ to XML之导航和查询
xml·solr·linq
残月心殇 请珍惜枸8 天前
LINQ之路17:LINQ to XML之X-DOM介绍
xml·solr·linq
海盗123410 天前
微软技术周报 ——2026-08-03
后端·python·microsoft·c#·.netcore
Volunteer Technology10 天前
Kafka的消费全流程
分布式·kafka·linq
「KISSSHOT」13 天前
抽象与性能:从 LINQ 看现代 .NET 的优化之道
java·.net·linq
香菜TTT13 天前
Kafka_深度解析_从架构原理到生产实践
分布式·架构·kafka·linq
明如正午14 天前
【C#】LINQ_HashSet_BitField解析
c#·linq
啊这啊这 六弦之首16 天前
.NET Core跨平台的奥秘[中篇]:复用之殇
.netcore