I'm doing a Pretty hefty LinqToSql statement that returns a new object. Due to the amount of SQL Methods (Sum and convert mainly) the SQL is taking a significantly long time to run and therefore loading the web page takes a long time (10-15 seconds). While I could use AJAX or similar with a CSS loader. I'm wondering first if there is a simple way to achieve what i am trying to get from the SQL Database.
I am trying to:
- Return all users where a given Field is not null
- Get all of the current items in the opportunities table where the status is 'open' and the Foreign Key matches. (after doing a manual join)
- Inside those opportunities, store the sum of all monetary values for several fields into my class
- Get the count of those monetary values.
The Linq Statement itself was a pretty long write, however when turned into SQL it is full of COALESCE and other hefty SQL methods.
Here is my LINQ statement:
decimal _default = (decimal)0.0000;
var users = from bio in ctx.tbl_Bios.Where(bio => bio.SLXUID != null)
join opp in ctx.slx_Opportunities.Where(opp => opp.STATUS == "open") on bio.SLXUID equals opp.ACCOUNTMANAGERID into opps
select new UserStats{
Name = bio.FirstName + " " + bio.SurName,
EnquiryMoney = opps.Where(opp => opp.SALESCYCLE == "Enquiry").Sum(opp => (opp.ACTUALAMOUNT.HasValue && opp.ACTUALAMOUNT.Value != _default ? opp.ACTUALAMOUNT : opp.SALESPOTENTIAL.HasValue ? (decimal)opp.SALESPOTENTIAL.Value : _default)).GetValueOrDefault(_default),
EnquiryNum = opps.Where(opp => opp.SALESCYCLE == "Enquiry").Count(),
GoingAheadMoney = opps.Where(opp => opp.SALESCYCLE == "Going Ahead").Sum(opp => (opp.ACTUALAMOUNT.HasValue && opp.ACTUALAMOUNT.Value != _default ? opp.ACTUALAMOUNT : opp.SALESPOTENTIAL.HasValue ? (decimal)opp.SALESPOTENTIAL.Value : _default)).GetValueOrDefault(_default),
GoingAheadNum = opps.Where(opp => opp.SALESCYCLE == "Going Ahead").Count(),
GoodPotentialMoney = opps.Where(opp => opp.SALESCYCLE == "Good Potential").Sum(opp => (opp.ACTUALAMOUNT.HasValue && opp.ACTUALAMOUNT.Value != _default ? opp.ACTUALAMOUNT : opp.SALESPOTENTIAL.HasValue ? (decimal)opp.SALESPOTENTIAL.Value : _default)).GetValueOrDefault(_default),
GoodPotentialNum = opps.Where(opp => opp.SALESCYCLE == "Good Potential").Count(),
LeadMoney = opps.Where(opp => opp.SALESCYCLE == "Lead").Sum(opp => (opp.ACTUALAMOUNT.HasValue && opp.ACTUALAMOUNT.Value != _default ? opp.ACTUALAMOUNT : opp.SALESPOTENTIAL.HasValue ? (decimal)opp.SALESPOTENTIAL.Value : _default)).GetValueOrDefault(_default),
LeadNum = opps.Where(opp => opp.SALESCYCLE == "Lead").Count(),
PriceOnlyMoney = opps.Where(opp => opp.SALESCYCLE == "Price Only").Sum(opp => (opp.ACTUALAMOUNT.HasValue && opp.ACTUALAMOUNT.Value != _default ? opp.ACTUALAMOUNT : opp.SALESPOTENTIAL.HasValue ? (decimal)opp.SALESPOTENTIAL.Value : _default)).GetValueOrDefault(_default),
PriceOnlyNum = opps.Where(opp => opp.SALESCYCLE == "Price Only").Count(),
ProvisionalMoney = opps.Where(opp => opp.SALESCYCLE == "Provisional").Sum(opp => (opp.ACTUALAMOUNT.HasValue && opp.ACTUALAMOUNT.Value != _default ? opp.ACTUALAMOUNT : opp.SALESPOTENTIAL.HasValue ? (decimal)opp.SALESPOTENTIAL.Value : _default)).GetValueOrDefault(_default),
ProvisionalNum = opps.Where(opp => opp.SALESCYCLE == "Provisional").Count()
};