Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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; }
}


What I have tried:

Can anyone please help me,

how to fill list with two columns in c#.

thanks
Posted
Updated 17-Sep-16 23:47pm
v6
Comments
Karthik_Mahalingam 18-Sep-16 4:56am    
where do you want to populate timeslotlist
abdul subhan mohammed 18-Sep-16 5:07am    
public List{TimeSlots} TimeSlotList
{
get { return _TimeSlots.Split(',').Select(DateTime.Parse).ToList(); }

set
{
TimeSlots = _TimeSlots.Remove(_TimeSlots.Length - 1);
}
}
Karthik_Mahalingam 18-Sep-16 5:33am    
why do you need DateTime.Parse, as you are using string type
abdul subhan mohammed 18-Sep-16 4:59am    
I want to populate in class only, as list using TimeSlot Class, get;set;
abdul subhan mohammed 18-Sep-16 5:06am    
public List{TimeSlots} TimeSlotList
{
get { return _TimeSlots.Split(',').Select(DateTime.Parse).ToList(); }

set
{
TimeSlots = _TimeSlots.Remove(_TimeSlots.Length - 1);
}
}

Try:
C#
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();
 
Share this answer
 
Comments
abdul subhan mohammed 18-Sep-16 5:59am    
Thanks Griff
try

C#
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('|');
       }
   }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900