Click here to Skip to main content
15,887,927 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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  //api response class
{
    public List<Table> Table { get; set; }
}


and this is the table class
public class Table  //api response class
{
    public List<Hours> hours { get; set; }
    public Table (List<Hours> _hours)
    {
      this.hours = _hours;
    }
}


public class Hours      //api response class
{
    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);  //table value has the data from SQL

foreach (TableValues tv in tableValues) //Tablevalues has the values of the record
{
     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
Posted
Updated 15-Apr-20 21:46pm
v9

I'll assume the numerous typos in the code you posted that will stop it from compiling are not the issue.

What you need to do is check if the day already exists and if so add the time to it and if not add it with a single time

C#
foreach (TableValue tv in tableValue)
{
    // see if the day already exists in the collection
    Hours existingDay = tableList.Table.Count == 0 ? null : tableList.Table.First().hours.FirstOrDefault(h => h.days == tv.days);

    // if not we'll add it
    if (existingDay == null)
    {
        List<Hours> hours = new List<Hours>
        {
            new Hours(tv.days, new List<string>{tv.time })
        };

        tableList.Table.Add(new Table(hours));
    }
    else
    {
        // the day already exists so we just need to add the time
        existingDay.time.Add(tv.time);
    }
}
 
Share this answer
 
Comments
Member 14800672 15-Apr-20 13:06pm    
I edited the typos and the response that i am currently getting ( i think i was mistaken when i first wrote it),
If you can see the edited one, i'm getting a new record with id=1 four times Hence (hour is repeated 4 times) instead of 1 hour and inside it are the different timings. So i'm not sure if this will affect the solution you provided.

I think i need to group by Id. so please help.

I will look at ur reply now and see if it will work
Member 14800672 15-Apr-20 13:37pm    
Just tried it it's not working
It's impossible to achieve json you expected due to tons of reason...

1.
The most important is that, that the input data does not contain corresponding values for such of output:
"day": "wednesday-sat" 
  "time": [
		closed
         ]

2.
In your project classes are badly designed, becuase they don't match to your json structure.

Take a look at here: Instantly parse JSON in any language | quicktype[^] to find out how your json string should look like and how to prepare corresponding classes.
Note: there you'll find only the part of your job. Feel free to improve that.
 
Share this answer
 
v2

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