使用LINQ C#的ArrayList组 [英] C# ArrayList group using Linq
本文介绍了使用LINQ C#的ArrayList组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前停留在这个地方。我需要一些帮助。
例如说,这是该方案的输入。
输入:周一至周五06:00-22:00,萨07:00-22:00,所以08:00-22:00
Formetet数据库表:
表名称:(开业)
-
日期| DayNumber |号码|打开|关闭
-
星期一,1,0:00,22:00
- 周二,2,晚上12点,22:00
- 星期三,3,晚上12点,22:00
- 周四,4,晚上12点,22:00
- 周五,5,0:00,22:00
- 周六,6,7:00,24:00
- 孙,0,8:00,24:00
我想白天的范围从该表中组
预期的输出会像:
时间:[
{
开放:{
天时:1,
时间:06:00
},
关: {
天时:5,
时间:22:00
}
},
{
开放:{
天时:6,
时间:07:00
},
关: {
天时:6,
时间:24:00
}
},
{
开放:{
天时:0,
时间:08:00
},
关: {
天时:0,
时间:24:00
}
}
]
解决方案
试试这个:
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
使用System.Data这;
使用的System.Xml;
使用的System.Xml.Serialization;
命名空间ConsoleApplication25
{
类节目
{ 静态无效的主要(字串[] args)
{
清单<串GT; DayNames中=新名单<串GT;(){墨子,涂,我们,TH,FR,SA,所以};
字符串输入=周一至周五06:00-22:00,萨07:00-22:00,所以08:00-22:00;
字符串[]天= input.Split(新的char [] {','}); 在天VAR dayRange =(从D
选择d.Split(新的char [] {''},StringSplitOptions.RemoveEmptyEntries))
。选择(X =>新建{天= X [0] .Trim(),时间= X [1] .Trim()})
。选择(X =>新建{
朝九特派= x.days.Contains( - )? x.days.Split(新的char [] {' - '})[0]:x.days,
endday指定= x.days.Contains( - )? x.days.Split(新的char [] {' - '})[1]:x.days,
STARTTIME = x.time.Contains( - )? x.time.Split(新的char [] {' - '})[0]:x.time,
ENDTIME = x.time.Contains( - )? x.time.Split(新的char [] {' - '})[1]:x.time,
}); VAR周期= dayRange.Select(X =>新建{
开放= {新的一天= dayNames.IndexOf(x.startDay)+ 1,时间= x.startTime},
关闭= {新的一天= dayNames.IndexOf(x.endDay)+ 1,时间= x.endTime}
}); 串formatedPeriods =的string.join(,,period.Select(X =>
{\\ n \\ r \\打开\\:{\\ n \\ r+
\\天\\:+ x.open.day.ToString()+,+
\\时\\:+ x.open.time.ToString()+\\ n \\ r},+
{\\ n \\ r \\关闭\\:{\\ n+
\\天\\:+ x.open.day.ToString()+,+
\\时\\:+ x.open.time.ToString()+\\ n \\ r} \\ n \\ r}
).ToArray());
字符串输出=的String.Format(\\时期\\:[\\ n \\ r {0} \\ n \\ r],formatedPeriods);
} }}
I am currently stuck at this place. I need some help.
Say for example this is the scenario input.
Input : Mo-Fr 06:00-22:00, Sa 07:00-22:00, So 08:00-22:00
Formetet to database Table:
Table name : (Opening)
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
I would like to group by day range from this table
The expected output will be like :
"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"
}
}
]
解决方案
Try this:
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 );
}
}
}
这篇关于使用LINQ C#的ArrayList组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文