C# 使用Linq的C ArrayList组

C# 使用Linq的C ArrayList组,c#,arrays,arraylist,group-by,sql-order-by,C#,Arrays,Arraylist,Group By,Sql Order By,我现在被困在这个地方。我需要一些帮助 例如,假设这是场景输入 输入:Mo Fr 06:00-22:00,Sa 07:00-22:00,So 08:00-22:00 Formetet到数据库表: 表名:期初 日|日数|日数|开|关 星期一,1,0:00,22:00 星期二,2,0:00,22:00 星期三,3,0:00,22:00 星期四,4,0:00,22:00 星期五,5,0:00,22:00 周六,6点,7点,24点 太阳,0,8:00,24:00 我想从这个表中按天分组 预期输出如下所示:

我现在被困在这个地方。我需要一些帮助

例如,假设这是场景输入

输入:Mo Fr 06:00-22:00,Sa 07:00-22:00,So 08:00-22:00

Formetet到数据库表:

表名:期初

日|日数|日数|开|关

星期一,1,0:00,22:00

星期二,2,0:00,22:00 星期三,3,0:00,22:00 星期四,4,0:00,22:00 星期五,5,0:00,22:00 周六,6点,7点,24点 太阳,0,8:00,24:00 我想从这个表中按天分组

预期输出如下所示:

"periods": [
        {
          "open": {
            "day": 1,
            "time": "06:00"
          },
          "close": {
            "day": 5,
            "time": "22:00"
          }
        },
        {
          "open": {
            "day": 6,
            "time": "07:00"
          },
          "close": {
            "day": 6,
            "time": "24:00"
          }
        },
        {
          "open": {
            "day": 0,
            "time": "08:00"
          },
          "close": {
            "day": 0,
            "time": "24:00"
          }
        }
      ]

为了做到这一点,我正在使用类似的东西。但我认为它无法满足我的确切需求:

如果有一个组具有相同的开盘-收盘日范围,则该代码将位于同一组中,且不存在差异

试试这个:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using System.Xml.Serialization;


namespace ConsoleApplication25
{
    class Program
    {

        static void Main(string[] args)
        {
            List<string> dayNames = new List<string>(){"Mo","Tu","We","Th","Fr","Sa","So"};
            string input = "Mo-Fr 06:00-22:00, Sa 07:00-22:00, So 08:00-22:00";
            string[] days = input.Split(new char[] { ',' });

            var dayRange = (from d in days
                            select d.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries))
                           .Select(x => new { days = x[0].Trim(), time = x[1].Trim() })
                           .Select(x => new {  
                               startDay = x.days.Contains("-") ? x.days.Split(new char[] {'-'})[0] : x.days,
                               endDay = x.days.Contains("-") ? x.days.Split(new char[] {'-'})[1] : x.days,
                               startTime = x.time.Contains("-") ? x.time.Split(new char[] {'-'})[0] : x.time,
                               endTime = x.time.Contains("-") ? x.time.Split(new char[] {'-'})[1] : x.time,
                           }); 

            var period = dayRange.Select(x => new {
                open = new {day = dayNames.IndexOf(x.startDay) + 1, time = x.startTime},
                close = new {day = dayNames.IndexOf(x.endDay) + 1, time = x.endTime}
            });

            string formatedPeriods = string.Join(",",period.Select(x => 
                "{\n\r\"open\": {\n\r" +
                "\"day\": " + x.open.day.ToString() + "," +   
                "\"time\": " + x.open.time.ToString() + "\n\r}," +   
               "{\n\r\"close\": {\n" +
                "\"day\": " + x.open.day.ToString() + "," +   
                "\"time\": " + x.open.time.ToString() + "\n\r}\n\r}"   
                ).ToArray());
            string output = string.Format("\"periods\": [\n\r{0}\n\r]",formatedPeriods );


        }

    }

}

非常感谢你的评论。这是一个很好的解决办法。但我需要这样的东西。我已经解析并将其存储到数据库中,这是我当前存储的表单“Day | DayNumber | Number | Open | Close Mon,1,0:00,22:00 Tue,2,0:00,22:00 Wed,3,0:00,22:00 Thu,4,0:00,22:00 Fri,5,0:00,22:00 Sat,6,7:00,24:00 Sun,0,8:00,24:00`我需要从中获取组数据。我编辑我的问题。实际上,我需要从这个表中获取格式化数据。@jdweng我已经编辑了代码段,因为代码段只能与HTML-CSS-JS三元组一起使用。对于其他语言,它们只是添加噪音而没有任何好处。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using System.Xml.Serialization;


namespace ConsoleApplication25
{
    class Program
    {

        static void Main(string[] args)
        {
            List<string> dayNames = new List<string>(){"Mo","Tu","We","Th","Fr","Sa","So"};
            string input = "Mo-Fr 06:00-22:00, Sa 07:00-22:00, So 08:00-22:00";
            string[] days = input.Split(new char[] { ',' });

            var dayRange = (from d in days
                            select d.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries))
                           .Select(x => new { days = x[0].Trim(), time = x[1].Trim() })
                           .Select(x => new {  
                               startDay = x.days.Contains("-") ? x.days.Split(new char[] {'-'})[0] : x.days,
                               endDay = x.days.Contains("-") ? x.days.Split(new char[] {'-'})[1] : x.days,
                               startTime = x.time.Contains("-") ? x.time.Split(new char[] {'-'})[0] : x.time,
                               endTime = x.time.Contains("-") ? x.time.Split(new char[] {'-'})[1] : x.time,
                           }); 

            var period = dayRange.Select(x => new {
                open = new {day = dayNames.IndexOf(x.startDay) + 1, time = x.startTime},
                close = new {day = dayNames.IndexOf(x.endDay) + 1, time = x.endTime}
            });

            string formatedPeriods = string.Join(",",period.Select(x => 
                "{\n\r\"open\": {\n\r" +
                "\"day\": " + x.open.day.ToString() + "," +   
                "\"time\": " + x.open.time.ToString() + "\n\r}," +   
               "{\n\r\"close\": {\n" +
                "\"day\": " + x.open.day.ToString() + "," +   
                "\"time\": " + x.open.time.ToString() + "\n\r}\n\r}"   
                ).ToArray());
            string output = string.Format("\"periods\": [\n\r{0}\n\r]",formatedPeriods );


        }

    }

}