如何用C#中的两列填充列表 [英] How to fill list with two columns in C#
本文介绍了如何用C#中的两列填充列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我在数据库SQL SERVER中有数据,我正在以字符串格式获取数据。
数据看起来像:08:00,11:00 | 11:00,13:00 | 13:00,16:00 |
如果我们看到,字符串用逗号和管道分隔。
逗号分隔值是Start&结束时间插槽,管道分隔值为
不同的时间段。
C类课程
Hi Guys,
I have data in Database SQL SERVER, i'm fetching data in string format.
data look like: "08:00,11:00|11:00,13:00|13:00,16:00|"
if we see, the string is separated by comma and pipe.
comma separated values are Start & End Time Slots where Pipe separated values are
different time slots.
Class in C#
public string _TimeSlots { get; set; }
public class TimeSlots
{
public string StartSlot { get; set; }
public string EndSlot { get; set; }
}
我的尝试:
任何人都可以帮助我,
如何用c#中的两列填充列表。
谢谢
What I have tried:
Can anyone please help me,
how to fill list with two columns in c#.
thanks
推荐答案
尝试:
Try:
string input = "08:00,11:00|11:00,13:00|13:00,16:00|";
List<TimeSlots> list = input.Split(new char[] {'|'}, StringSplitOptions.RemoveEmptyEntries)
.Select(ts => { string[] parts = ts.Split(',');
return new TimeSlots { StartSlot = parts[0],
EndSlot = parts[1] }; })
.ToList();
尝试
try
public List<TimeSlots> TimeSlotList
{
get { return
_TimeSlots.Split('|').Select(k=> { var temp = k.Split(','); return new TimeSlots(){ StartSlot =temp[0], EndSlot =temp[1] } }).ToList();
}
set{
_TimeSlots = "";
value.ForEach(k => { _TimeSlots += k.StartSlot + "," + k.EndSlot + "|"; });
_TimeSlots = _TimeSlots.TrimEnd('|');
}
}
这篇关于如何用C#中的两列填充列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文