c# - How to improve Linq-to-sql in MVC action method -


i'm working on method generate pdf. purpose i'm using tuespechkin library . conversion quite fast. of time takes retrieving data database , creating html representation of pdf. improve performance tried use async methods , plinq not help. there way optimize method?

most expensive part of code(updated):

stringbuilder html = new stringbuilder(1024 * 1024); // adapt memory reservation depending on document size //---------------------------- start page -----------------------------------------------------------// imagearray = system.io.file.readallbytes(httpcontext.server.mappath("../content/images/pdf_logo.jpg")); base64imagerepresentation = convert.tobase64string(imagearray); html.append("<div class='break' style='margin: 0 0 0 0px;'>");    html.append("<img class='report-img' style='margin:150px 0 400px 0;' src='data:image/jpeg;base64," + base64imagerepresentation + "'></img>");    html.append("<p class='purpletext' style='color:#8e0033; font-size: 30px; margin:0; line-height:20px;'>" + title + "</p>" +             "<p class='purpletext' style='color:#8e0033; font-size: 16px;'>" + bigtitle + "</p>");    html.append("<p style='color:#980000; font-size: 50px; margin: 46px 0 0 0;'>" + custname.firstname + " <span>" + custname.lastname + "</span></p>"); html.append("</div>");  //------------------------------------ 2nd page -----------------------------------------------------------// html.append("<div class='text-block break'></div>");  //=======================================================================================================================// //------------------------------------- generate menu (p4) ----------------------------------------------------------// html.append(header); html.append("<span  class='menu-title'>innholdsfortegnelse</span>"); html.append("<ul class='content-list break'>");  //------------------ innlednings menu --------------------------// if (await db.innlednings.anyasync(n => n.customerid == customerid)) {    html.append("<li class='list-header'>innledning</li>"); } //------------------ fordelings menu --------------------------// if (await db.fordelings.anyasync(n => n.customerid == customerid)) {    html.append("<li class='list-header'>fordeling av bruk og regninger</li>"); } //--------------------------------- report area bruk menu ----------------------------------------------------// //--------------------------- transaction category id of selected area ---------------------------// const string trancatbruk = "bruk"; var trancatidbruk = 0; if (await db.transactioncategories.anyasync(n => n.name == trancatbruk)) {     trancatidbruk = (await (db.transactioncategories.firstasync(n => n.name == trancatbruk))).id; } else {     throw new httpexception(404, "'" + trancatbruk + "' transaction category not exist!"); } if (await db.reportviews.anyasync(n => n.customerid == customerid && n.transactioncategoryid == trancatidbruk)) {    html.append("<li class='list-header'>brukskonto</li>");      if (await db.fordelingbruks.anyasync(n => n.customerid == customerid))     {        html.append("<li class='list-level2'>fordeling av brukskonto</li>");     }      var rvbrukitems = await         db.reportviews.where(n => n.customerid == customerid && n.transactioncategoryid == trancatidbruk).tolistasync();     foreach (var rvitem in rvbrukitems)     {         var maincatname = (await db.categories.firstasync(n => n.id == rvitem.maincategoryid)).name;          if (!rvitem.totalpermonthimage.isnullorwhitespace() ||             !rvitem.differentstoresimage.isnullorwhitespace() ||             !rvitem.subcategoryimage.isnullorwhitespace() ||             !rvitem.transactionspermonthimage.isnullorwhitespace() ||             !rvitem.averagetradevalueimage.isnullorwhitespace())         {            html.append("<li class='list-level2'>" + maincatname + "</li>");         }         //----------------- images --------------------------//         if (!rvitem.totalpermonthimage.isnullorwhitespace())         {            html.append("<li class='list-level3'>totalt pr. måned</li>");         }         if (!rvitem.differentstoresimage.isnullorwhitespace())         {            html.append("<li class='list-level3'>fordeling</li>");         }         if (!rvitem.subcategoryimage.isnullorwhitespace())         {            html.append("<li class='list-level3'>kategorier</li>");         }         if (!rvitem.transactionspermonthimage.isnullorwhitespace())         {            html.append("<li class='list-level3'>transaksjoner pr. måned</li>");          }         if (!rvitem.averagetradevalueimage.isnullorwhitespace())         {            html.append("<li class='list-level3'>gjennomsnittlig handlebeløp</li>");          }      }  } //--------------------------------- report area regning menu ----------------------------------------------------// //--------------------------- transaction category id of selected area ---------------------------// const string trancatregning = "regning"; var trancatidregning = 0; if (await db.transactioncategories.anyasync(n => n.name == trancatregning)) {     trancatidregning = (await db.transactioncategories.firstasync(n => n.name == trancatregning)).id;  } else {     throw new httpexception(404, "'" + trancatregning + "' transaction category not exist!"); } if (await db.reportviews.anyasync(n => n.customerid == customerid && n.transactioncategoryid == trancatidregning)) {    html.append("<li class='list-header'>regningskonto</li>");      if (await db.fordelingbruks.anyasync(n => n.customerid == customerid))     {        html.append("<li class='list-level2'>fordeling av regningskonto</li>");     }      var rvbrukitems = await          db.reportviews.where(n => n.customerid == customerid && n.transactioncategoryid == trancatidregning).tolistasync();      foreach (var rvitem in rvbrukitems)     {         var maincatname = (await db.categories.firstasync(n => n.id == rvitem.maincategoryid)).name;         if (!rvitem.totalpermonthimage.isnullorwhitespace() ||             !rvitem.differentstoresimage.isnullorwhitespace() ||             !rvitem.subcategoryimage.isnullorwhitespace() ||             !rvitem.transactionspermonthimage.isnullorwhitespace() ||             !rvitem.averagetradevalueimage.isnullorwhitespace())         {            html.append("<li class='list-level2'>" + maincatname + "</li>");         }         //----------------- images --------------------------//         if (!rvitem.totalpermonthimage.isnullorwhitespace())         {            html.append("<li class='list-level3'>totalt pr. måned</li>");         }         if (!rvitem.differentstoresimage.isnullorwhitespace())         {            html.append("<li class='list-level3'>fordeling</li>");         }         if (!rvitem.subcategoryimage.isnullorwhitespace())         {            html.append("<li class='list-level3'>kategorier</li>");         }         if (!rvitem.transactionspermonthimage.isnullorwhitespace())         {            html.append("<li class='list-level3'>transaksjoner pr. måned</li>");          }         if (!rvitem.averagetradevalueimage.isnullorwhitespace())         {            html.append("<li class='list-level3'>gjennomsnittlig handlebeløp</li>");          }      }  } //------------------ inntekt menu --------------------------// html.append("<li class='list-header'>inntekt</li>"); if (await db.inntektpermonths.anyasync(n => n.customerid == customerid)) {    html.append("<li class='list-level2'>inntekt per måned</li>"); } if (await db.inntektsubcategories.anyasync(n => n.customerid == customerid)) {    html.append("<li class='list-level2'>fordeling av inntekt</li>"); } if (await db.inntektfordelings.anyasync(n => n.customerid == customerid)) {    html.append("<li class='list-level2'>fordeling mellom inntekt og forbruk</li>"); }  //----------------- sparing menu ------------------------// if (db.sparingkommentarers.any(n => n.customerid == customerid) ||     db.sparingtabells.any(n => n.customerid == customerid)) {    html.append("<li class='list-header'>sparing</li>");      if (await db.sparingkommentarers.anyasync(n => n.customerid == customerid))     {        html.append("<li class='list-level2'>kommentar</li>");     }     if (await db.sparingrads.anyasync(n => n.customerid == customerid))     {        html.append("<li class='list-level2'>sparetips</li>");     }     if (await db.sparingtabells.anyasync(n => n.customerid == customerid))     {        html.append("<li class='list-level2'>tabell</li>");     } } //----------------- budsjett menu ------------------------// if (db.budsjettkommentarers.any() ||     db.budsjettgraf1s.any() ||     db.totalbruks.any() ||     db.totalregningers.any()) {    html.append("<li class='list-header'>budsjett</li>");      if (await db.budsjettkommentarers.anyasync(n => n.customerid == customerid))     {        html.append("<li class='list-level2'>kommentar</li>");     }     if (await db.totalbruks.anyasync(n => n.customerid == customerid))     {        html.append("<li class='list-level2'>totalt bruk</li>");     }     if (await db.totalregningers.anyasync(n => n.customerid == customerid))     {        html.append("<li class='list-level2'>total regninger</li>");     }     if (await db.budsjettgraf1s.anyasync(n => n.customerid == customerid))     {        html.append("<li class='list-level2'>budsjett tall</li>");     } } //----------------- oppsummering(articles) menu ------------------------// if (await db.articles.anyasync(n => n.customerid == customerid)) {    html.append("<li class='list-header'>oppsummering</li>"); }  //------------------ end of menu -----------------------------------------------// html.append("</ul>");  //------------------ 2 blank pages (6-7) ---------------------------------------//   ////------------------------------- start generating inner pdf ---------------------------------------------// ////---------------------------------------innledning------------------------------------------------------// var customerforinnlending = await db.innlednings.firstordefaultasync(inl => inl.customerid == customerid); html.append(header);  string textforinnlending = (customerforinnlending != null) ? customerforinnlending.text : "<b>empty text</b>";  html.append("<h1 class='menu-title'>innledning </h1>" +         "<div class='text-block'>" + textforinnlending + "</div>" +         "<p class='break'></p>"); html.append("<br>");   //-----------------------------------fordeling-----------------------------------------------------------// var fordelinglist = await db.fordelings.where(cus => cus.customerid == customerid).tolistasync(); foreach (var item in fordelinglist) {     if (!item.transactionsimage.isnullorwhitespace())     {        html.append(header);        html.append("<h1> fordeling av bruk og regninger </h1>");        html.append("<img class='report-img circlediagram' width='800px' height='auto' src='" + item.transactionsimage + "'></img>");        html.append("<div class='text-block'>" + item.text + "</div>");     }     if (!item.toptransactionsimage.isnullorwhitespace())     {        html.append("<img class='report-img stolpeddiagram mtop30' width='600px' height='auto' src='" + item.toptransactionsimage + "'></img>");     }    html.append("<div class='text-block break'></div>");  }  //---------------------------- bruskonto title -----------------------------------------------------------// imagearray = system.io.file.readallbytes(httpcontext.server.mappath("../content/images/bruk_header.png")); base64imagerepresentation = convert.tobase64string(imagearray); html.append(header); html.append("<div class='break' style='display:block;padding-top:300px;padding-left:0px;'>"); html.append("<img class='report-img' src='data:image/png;base64," + base64imagerepresentation + "'></img>"); html.append("</div>");    ////------------------------------------------------fordeling av bruk------------------------------------------------------// var fordelingbruk = await db.fordelingbruks.where(cus => cus.customerid == customerid).tolistasync(); foreach (var item in fordelingbruk) {     if (!item.maincatimage.isnullorwhitespace())     {        html.append(header);        html.append("<h1>fordeling av brukskonto </h1>");        html.append("<img class='report-img circlediagram' src='" + item.maincatimage + "'></img>");        html.append("<div class='text-block'>" + item.text + "</div>");     }     if (!item.topmaincatimage.isnullorwhitespace())     {        html.append("<img class='report-img stolpeddiagram mtop30' width='600px' height='auto' src='" + item.topmaincatimage + "'></img>");     }    html.append("<div class='text-block break'></div>"); }     //------------------------------------------------ reportview bruk------------------------------------------------------// //--------------------------- transaction category id of selected area ---------------------------// var trancat = "bruk"; var trancatid = 0; if (await db.transactioncategories.anyasync(n => n.name == trancat)) {     trancatid = (await db.transactioncategories.firstasync(n => n.name == trancat)).id;  } else {     throw new httpexception(404, "'" + trancat + "' transaction category not exist!");  }  var reportviewbruklist = db.reportviews.asparallel().where(n => n.transactioncategoryid == trancatid && n.customerid == customerid).tolist();  foreach (var rvitem in reportviewbruklist) {     var maincatname = (await db.categories.firstasync(n => n.parent == null && n.id == rvitem.maincategoryid)).name;     //----------------- images --------------------------//     if (!rvitem.totalpermonthimage.isnullorwhitespace())     {        html.append(header);        html.append("<h1> diverse " + maincatname + "</h1>");        html.append("<img class='report-img img-size' src='" + rvitem.totalpermonthimage + "'></img>");        html.append("<div class='text-block break'>" + rvitem.totalpermonthtext + "</div>");     }     if (!rvitem.differentstoresimage.isnullorwhitespace())     {        html.append(header);        html.append("<h1> fordeling av " + maincatname + "</h1>");        html.append("<img class='report-img img-size' src='" + rvitem.differentstoresimage + "'></img>");        html.append("<div class='text-block break'>" + rvitem.differentstorestext + "</div>");       }     if (!rvitem.subcategoryimage.isnullorwhitespace())     {        html.append(header);        html.append("<h1> fordeling på " + maincatname + "</h1>");        html.append("<img class='report-img img-size' src='" + rvitem.subcategoryimage + "'></img>");        html.append("<div class='text-block break'>" + rvitem.subcategorytext + "</div>");     }     if (!rvitem.transactionspermonthimage.isnullorwhitespace())     {        html.append(header);        html.append("<h1>" + maincatname + "</h1>");        html.append("<img class='report-img img-size' src='" + rvitem.transactionspermonthimage + "'></img>");        html.append("<div class='text-block break'>" + rvitem.transactionspermonthtext + "</div>");      }     if (!rvitem.averagetradevalueimage.isnullorwhitespace())     {        html.append(header);        html.append("<h1> antall transaksjoner pr. mnd på " + maincatname + "</h1>");        html.append("<img class='report-img img-size' src='" + rvitem.averagetradevalueimage + "'></img>");        html.append("<div class='text-block break'>" + rvitem.averagetradevaluetext + "</div>");      }  }  //---------------------------- regningskonto title -----------------------------------------------------------// imagearray = system.io.file.readallbytes(httpcontext.server.mappath("../content/images/regninger_header.png")); base64imagerepresentation = convert.tobase64string(imagearray); html.append(header); html.append("<div class='break' style='display:block;padding-top:300px;padding-left:0px;'>"); html.append("<img class='report-img' src='data:image/png;base64," + base64imagerepresentation + "'></img>"); html.append("</div>");  //-------------------------------------------- fordeling av regninger (page 39) ---------------------------------------------------------// var fordelingregninger = await db.fordelingregningers.where(cus => cus.customerid == customerid).tolistasync(); foreach (var item in fordelingregninger) {     if (!item.maincatimage.isnullorwhitespace())     {        html.append(header);        html.append("<h1>fordeling av regninger </p>");        html.append("<img class='report-img circlediagram' width='800px' height='auto' src='" + item.maincatimage + "'></img>");        html.append("<div class='text-block'>" + item.text + "</div>");     }     if (!item.topmaincatimage.isnullorwhitespace())     {        html.append("<img class='report-img stolpeddiagram mtop30' width='600px' height='auto' src='" + item.topmaincatimage + "'></img>");     }    html.append("<div class='text-block break'></div>"); }   //------------------------------------------------ reportview regninger (p38-48)------------------------------------------------------// //--------------------------- transaction category id of selected area ---------------------------// trancat = "regning"; trancatid = 0; if (await db.transactioncategories.anyasync(n => n.name == trancat)) {     trancatid = (await db.transactioncategories.firstasync(n => n.name == trancat)).id;  } else {     throw new httpexception(404, "'" + trancat + "' transaction category not exist!"); }  var reportviewregningerlist = db.reportviews.asparallel().where(n => n.transactioncategoryid == trancatid && n.customerid == customerid).tolist();  foreach (var rvitem in reportviewregningerlist) {     var maincatname = (await db.categories.firstasync(n => n.parent == null && n.id == rvitem.maincategoryid)).name;     //----------------- images --------------------------//     if (!rvitem.totalpermonthimage.isnullorwhitespace())     {        html.append(header);        html.append("<h1> diverse " + maincatname + "</h1>");        html.append("<img class='report-img img-size' src='" + rvitem.totalpermonthimage + "'></img>");        html.append("<div class='text-block break'>" + rvitem.totalpermonthtext + "</div>");     }     if (!rvitem.differentstoresimage.isnullorwhitespace())     {        html.append(header);         html.append("<h1> fordeling av " + maincatname + "</h1>");        html.append("<img class='report-img img-size' src='" + rvitem.differentstoresimage + "'></img>");        html.append("<div class='text-block break'>" + rvitem.differentstorestext + "</div>");       }     if (!rvitem.subcategoryimage.isnullorwhitespace())     {        html.append(header);        html.append("<h1> fordeling på " + maincatname + "</h1>");        html.append("<img class='report-img img-size' src='" + rvitem.subcategoryimage + "'></img>");        html.append("<div class='text-block break'>" + rvitem.subcategorytext + "</div>");     }     if (!rvitem.transactionspermonthimage.isnullorwhitespace())     {        html.append(header);        html.append("<h1>" + maincatname + "</h1>");        html.append("<img class='report-img img-size' src='" + rvitem.transactionspermonthimage + "'></img>");        html.append("<div class='text-block break'>" + rvitem.transactionspermonthtext + "</div>");      }     if (!rvitem.averagetradevalueimage.isnullorwhitespace())     {        html.append(header);        html.append("<h1> antall transaksjoner pr. mnd på " + maincatname + "</h1>");        html.append("<img class='report-img img-size' src='" + rvitem.averagetradevalueimage + "'></img>");        html.append("<div class='text-block break'>" + rvitem.averagetradevaluetext + "</div>");      }  }   //---------------------------- inntekter title -----------------------------------------------------------// imagearray = system.io.file.readallbytes(httpcontext.server.mappath("../content/images/inntekter_header.png")); base64imagerepresentation = convert.tobase64string(imagearray); html.append(header); html.append("<div class='break' style='display:block;padding-top:300px;padding-left:0px;'>"); html.append("<img class='report-img' src='data:image/png;base64," + base64imagerepresentation + "'></img>"); html.append("</div>");  //---------------------------------------- inntekt per måned  (p50) --------------------------------------------------------------// if (await db.inntektpermonths.anyasync(n => n.customerid == customerid)) {     var innpermaned = await db.inntektpermonths.firstasync(n => n.customerid == customerid);     if (!innpermaned.inntektpermonthimage.isnullorwhitespace())     {        html.append(header);        html.append("<h1> inntekt per måned </h1>");        html.append("<img class='report-img img-size' src='" + innpermaned.inntektpermonthimage + "'></img>");        html.append("<div class='text-block break'>" + innpermaned.text + "</div>");      }  }  //---------------------------------------- fordeling av inntekt  (p51) --------------------------------------------------------------// if (await db.inntektfordelings.anyasync(n => n.customerid == customerid)) {     var innfordelings = await db.inntektsubcategories.firstasync(n => n.customerid == customerid);     if (!innfordelings.inntektsubcategoryimage.isnullorwhitespace())     {        html.append(header);        html.append("<h1> fordeling av inntekt  </h1>");        html.append("<img class='report-img img-size' src='" + innfordelings.inntektsubcategoryimage + "'></img>");        html.append("<div class='text-block break'>" + innfordelings.text + "</div>");      } }   //---------------------------------------- fordeling mellom inntekt og forbruk   (p52) --------------------------------------------------------------// if (await db.inntektsubcategories.anyasync(n => n.customerid == customerid)) {     var innsub = await db.inntektfordelings.firstasync(n => n.customerid == customerid);     if (!innsub.inntektfordelingimage.isnullorwhitespace())     {        html.append(header);        html.append("<h1> fordeling mellom inntekt og forbruk  </h1>");        html.append("<img class='report-img img-size' src='" + innsub.inntektfordelingimage + "'></img>");        html.append("<div class='text-block break'>" + innsub.text + "</div>");      } }  html.append("</body></html>");  //---------- code  //........  var htmlstr = html.tostring(); 

strings immutable in .net. should use stringbuilder instead of string.

something that:

stringbuilder sb = new stringbuilder(16 * 1024); // adapt memory reservation depending on document size sb.append("string_to_append"); ... return sb.tostring(); 

this technique far more effective +=


Comments