I have been trying to compile data from a table that looks like image1 and convert and display it to a table like image2.
Image1
Image2
First I figured I would do like this: One class called Region that contains a list of the class called server The server class would have 3 properties serverid, servername and a list of the class called CostData. CostData would store year-month and sum of the cost for the month.
I have managed with linq to get a query that gives me the total for each server per month, like: Year 2013, Month: 1, Server: Server1, Total: 460
Still I find myself short of a few things. First I need to put each servers monthly totals in one place, like what I've tried with my CostData, so that I with html can just iterate each servers month and display them in columns.
Also, with this solution I have still missed the CostDesc column which I also want to display the monthly total for like you can se on image2.
Here are my classes:
public class Region
{
public List<Server> Servers { get; set; }
}
public class Server
{
public string ServerID { get; set; }
public string ServerName { get; set; }
public List<CostData> MonthlyCosts { get; set; }
}
public class CostData
{
public string Date { get; set; }
public double Sum { get; set; }
}
This is how I am building my objects:
Region r = new Region();
r.servers = new List<Server>();
foreach (var row in linqQueryResult)
{
Server s = new Server();
s.ServerID = row.ServerID;
s.ServerName = row.ServerName;
s.MonthlyCosts = new List<CostData>();
CostData cd = new CostData();
cd.Date = row.Year.ToString() + "-" + row.Month.ToString();
cd.Sum = row.ServerSum;
s.MonthlyCosts.Add(cd);
r.Servers.Add(s);
}
return View(r);
Do you have any pointers or suggestions? I'm hoping somebody with more experience could take a look and give me some advice, not asking for the entire solution although if you wish to do it I would not mind :)
I wonder if there isn't a good way to get the data I want using linq. The catch to this is that I only have access to a stored procedure that will only give me data like in image1. All the manipulation with the data must be done locally with c#, although that probably isn't such a bad thing.