Let's say i have this
Id Day Time
1 Sun 8am to 4pm
1 Sun 5pm to 9pm
1 Mon 8m to 6pm
1 Tue 8m to 6pm
2 Sun 8am to 4pm
2 Mon 8am to 4pm
I want to make an api response in .net framework ,the
expected result is this when i choose id=1
{
"Tables": [
{
"hours":
[
{
"days": "Sun"
"time": [
"8am to 4pm",
"5pm to 9pm"
]
}
{
"day": Mon - Tue
"time":[ 8am to 6pm]
}
{
"day": "wednesday-sat"
"time": [
closed
]
}
]
}
]
}
What I have tried:
I have this TableList class:
public class TableList
{
public List<Table> Table { get; set; }
}
and this is the table class
public class Table
{
public List<Hours> hours { get; set; }
public Table (List<Hours> _hours)
{
this.hours = _hours;
}
}
public class Hours
{
public string days { get; set; }
public <string> time { get; set; }
public Hours(string days, <string> time)
{
this.days = days;
this.time = time;
}
}
TableList tableList = new TableList ();
TableList.Table= new List<Table>();
List<TableValue> tableValue= service.getTime(1);
foreach (TableValues tv in tableValues)
{
List<string> hourtime= new List<string>
{
new string(tv.hours.ToCharArray())
};
List<Hours> hours = new List<Hours>
{
new Hours(tv.days, hourtime)
};
TableList.Table.Add(new Table(hours))
}
But what i'm currently getting is
{
"Tables": [
{
"hours":
[
{
"days": "Sun"
"time":
["8am to 4pm"]
}
]
},
{
"hours":
[
{
"days": "Sun"
"time":
["5pm to 9pm"]
}
]
},
{
"hours":
[
{
"day": Mon
"time": [8am to 6pm]
}
]
},
{
"hours":
[
{
"day": Tue
"time":[ 8am to 6pm]
}
]
}
]
}
i want to get the Hours Id=1 once and inside it are the days and timing. I don't want new array for each new timing of the same Id. I also don't want sunday to come in two records. Also want if more than one day has the same time then there is no need to show in new array i want it like the expected result i showed u above above. and i want to implement "closed" as well