Is there a way to reduce this down

Associate
Joined
19 Jul 2006
Posts
1,847
OK I'm writing reports for a database I have

Code:
                DateTime startDate = DateTime.Parse(start);
                DateTime endDate = DateTime.Parse(end);
                ViewData["TotalDuringPeriod"] = db.AlcoholForms.Count(x => x.CreatedDate >= startDate && x.CreatedDate <= endDate);
                ViewData["TotalUniqueClientsDuringPeriod"] = db.AlcoholForms.Where(x => x.CreatedDate >= startDate && x.CreatedDate <= endDate).Select(x => x.ClientId).Distinct().Count();
                ViewData["TotalGateNew"] = db.AlcoholForms.Count(x => x.ReferredTo == "Gate" && x.Reengagement == false && (x.CreatedDate >= startDate && x.CreatedDate <= endDate));
                ViewData["TotalGateReengagment"] = db.AlcoholForms.Count(x => x.ReferredTo == "Gate" && x.Reengagement && (x.CreatedDate >= startDate && x.CreatedDate <= endDate));
                ViewData["TotalSwitchNew"] = db.AlcoholForms.Count(x => x.ReferredTo == "Switch" && x.Reengagement == false && (x.CreatedDate >= startDate && x.CreatedDate <= endDate));
                ViewData["TotalSwitchReengagement"] = db.AlcoholForms.Count(x => x.ReferredTo == "Switch" && x.Reengagement && (x.CreatedDate >= startDate && x.CreatedDate <= endDate));
                List<ReportAlcoholReferals> reportData = db.AlcoholForms.Where(x => x.Referral && (x.CreatedDate >= startDate && x.CreatedDate <= endDate)).Select(x => new ReportAlcoholReferals()
                {
                    FirstName = x.Client.FirstName,
                    LastName = x.Client.LastName,
                    Date = x.CreatedDate,                 
                    ClientId = x.ClientId,
                    AlcoholForm = x
                }).OrderBy(x => x.AlcoholForm.ReferredTo).ToList();
                return View(reportData);

Everything works, but is there a way to make it more efficient as i'm hitting the DB 7 times?

Cheers
 
Associate
OP
Joined
19 Jul 2006
Posts
1,847
Thanks for the advice guys,

Never done a stored procedure before, may look into them in the future, but after talking this through with my mate. The program is never going to get to the size where this will be an issue
 
Back
Top Bottom