I have a settings table with two columns, "Name" and "Value":
Name | Value
-----------+-------
start_time | 9:30
end_time | 4:30
length | 30
I also have a result set that gives the number of appointments per time period:
count | time
------+--------
1 | 09:30
2 | 12:00
3 | 14:00
4 | 15:30
And I need to show all the time slots between start_time and end_time, with their number of appointments like this:
(1) 09:30
10:30
11:00
11:30
(2) 12:00
12:30
13:00
13:30
(3) 14:00
14:30
15:00
(4) 15:30
Here's what I got:
// data is the result of WebMatrix.Data.Database.Query
public void PrepareHTML(dynamic data)
{
StringBuilder SB = new StringBuilder("");
TimeSpan time = StartTime; // Start/EndTime are `TimeSpan`s defined in another place
// loop from StartTime to EndTime in 30 min increments
while(time <= EndTime)
{
String t = String.Format(@"{0:hh\:mm}", time);
try
{
var d = GetThisTime(t,data);
SB.AppendFormat("<option value=\"{0}\">({1}) {0}</option>", t, d["count"]); // error is here: Cannot apply indexing with [] to an expression of type 'object'
}
catch
{
SB.AppendFormat("<option value=\"{0}\">{0}</option>", t);
}
time = time.Add(Length);
}
H = SB.ToString();
}
public IEnumerable<dynamic> GetThisTime(String time, IEnumerable<dynamic> data)
{
return from a in data
where time.CompareTo(new TimeSpan(a.time)) == 0
select a;
}
Update: The 'error' I put in comments was not apparent, so I will duplicate here: I get an error at
SB.AppendFormat("<option value=\"{0}\">({1}) {0}</option>", t, d["count"]);
The error is:
Cannot apply indexing with [] to an expression of type 'object'
Since it seems to be unclear: I do not want the count of the IEnumerable, I want the count column of the data set. See the second table above.
GetThisTimereturnIEnumerable, so it collection, if you want Count of them you need use linq Count method liked.Count()// error is here: Cannot apply indexing with [] to an expression of type 'object'GetThisTimeis, it's impossible to know how to provide any help. Have you tried justd.Count?