Skip to content

Instantly share code, notes, and snippets.

@rnikitin
Created December 6, 2012 13:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rnikitin/4224486 to your computer and use it in GitHub Desktop.
Save rnikitin/4224486 to your computer and use it in GitHub Desktop.
Linq to Entities Roocks
public virtual ActionResult Form(Guid appID, Guid id)
{
var form = _repo.FormsRepository.Get(id);
var app = _repo.Apps.Get(appID);
if (form.App.Company.ID != CurrentContext.Customer.Company.ID)
throw new HttpException((int)HttpStatusCode.Forbidden, "Not authorized access");
var model = new FormViewModel
{
Form = form,
App = app
};
var formsRepository = new FormDataRepository();
var tickets = formsRepository.QueryTickets(CurrentContext.Customer.Company.ID, formId: form.ID, afterDate: DateTime.UtcNow.AddDays(-30));
var ticketData = formsRepository.QueryTicketData(CurrentContext.Customer.Company.ID, formId: form.ID, afterDate: DateTime.UtcNow.AddDays(-30));
// return as is, there is no data
if (tickets.Count == 0)
return View(model);
// replace all ClosedAt null values with current data
foreach (var t in tickets.Where(t => !t.ClosedAt.HasValue))
{
t.ClosedAt = DateTime.UtcNow;
}
// calculate basic stats
model.NewTickets = tickets.Count(x => x.Status == TicketStatus.New);
model.OpenTickets = tickets.Count(x => x.Status == TicketStatus.Open);
model.ClosedTickets = tickets.Count(x => x.Status == TicketStatus.Closed);
// calculate avg answer time
model.AvgAnswerTime = tickets.Average(x => (x.ClosedAt - x.Created).Value.TotalSeconds);
// group tickets by date for big graph
model.TicketsByDate = (from t in tickets
group t by t.Created.Date
into g
select new Group<DateTime, TicketEntry> { Key = g.Key, Data = g.ToList() }).ToList();
// group ticketData
model.TicketDataByNameValue = (from tData in ticketData.Where(x => x.Value.Length <= 32)
group tData by tData.Name
into group1
select new Group<string, Group<string, TicketDataEntry>>
{
Key = group1.Key,
Data = (from group1Data in group1.ToList()
group group1Data by group1Data.Value
into group2
select new Group<string, TicketDataEntry>
{
Key = group2.Key,
Data = group2
}).ToList()
}).ToList();
return View(model);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment