I am creating my first MVC5 / EF / LINQ application and I would like to display the results of several (about 75) "Select Count(*)" queries in one view. The purpose is to display different statistics from a single table on a home page "dashboard", along with a list of "active" items from the same table in the database.
I have read different opinions on how the best way run select county queries in LINQ... What is the most efficient way to do this considering how many queries I need to run on the page?
A sample query would be
select count(*) from TableName WHERE LType="A" AND (LDate BETWEEN 02/10/2015 AND 02/25/2015)
public ActionResult Index(string sortOrder, string currentFilter, string searchString, int? page)
{
ViewBag.CurrentSort = sortOrder;
ViewBag.LastNameSortParam = String.IsNullOrEmpty(sortOrder) ? "LastName" : "";
ViewBag.DateSortParam = sortOrder == "Date" ? "LeadDate" : "Date";
if (searchString != null)
{
page = 1;
}
else
{
searchString = currentFilter;
}
ViewBag.CurrentFilter = searchString;
var newLeads = from l in db.WebLeads
where (l.LoanAgent == null || l.LoanAgent == "hb@mwfinc.com")
select l;
if (!String.IsNullOrEmpty(searchString))
{
newLeads = newLeads.Where(l => l.LastName.Contains(searchString)
|| l.FirstName.Contains(searchString));
}
switch (sortOrder)
{
case "Date":
newLeads = newLeads.OrderBy(l => l.LeadDate);
break;
case "LeadDate":
newLeads = newLeads.OrderByDescending(l => l.LeadDate);
break;
case "LastName":
newLeads = newLeads.OrderBy(l => l.LastName);
break;
default:
newLeads = newLeads.OrderByDescending(l => l.LeadDate);
break;
}
int pageSize = 15;
int pageNumber = (page ?? 1);
DateTime minDate = System.DateTime.Today;
DateTime maxDate = System.DateTime.Now;
// Leads to Be Assigned Daily Count
var ctPurchTBA = db.WebLeads.Count(x => (x.LoanType == "Home Purchase" || x.LoanType == "CalPATH Home Purchase")
&& x.LeadDate >= minDate
&& x.LeadDate <= maxDate);
return View(newLeads.ToPagedList(pageNumber, pageSize));
}
'