title: How to improve Linq-to-sql in MVC action method
body: <p>I'm working on a method to generate pdf. For this purpose I'm using tuespechkin library . Conversion itself is quite fast. Most of the time it takes retrieving data from the database and creating html representation of pdf.
  To improve the performance I tried to use async methods and PLinq but it is not much help.
  Is there a way to optimize this method?</p>
  
  <p>most expensive part of the code(Updated):</p>
  
  <pre><code>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("&lt;div class='break' style='margin: 0 0 0 0px;'&gt;");
     html.Append("&lt;img class='report-img' style='margin:150px 0 400px 0;' src='data:image/jpeg;base64," + base64ImageRepresentation + "'&gt;&lt;/img&gt;");
     html.Append("&lt;p class='purpleText' style='color:#8E0033; font-size: 30px; margin:0; line-height:20px;'&gt;" + title + "&lt;/p&gt;" +
              "&lt;p class='purpleText' style='color:#8E0033; font-size: 16px;'&gt;" + bigTitle + "&lt;/p&gt;");
     html.Append("&lt;p style='color:#980000; font-size: 50px; margin: 46px 0 0 0;'&gt;" + custName.firstName + " &lt;span&gt;" + custName.lastName + "&lt;/span&gt;&lt;/p&gt;");
  html.Append("&lt;/div&gt;");
  
  //------------------------------------ 2nd page -----------------------------------------------------------//
  html.Append("&lt;div class='text-block break'&gt;&lt;/div&gt;");
  
  //=======================================================================================================================//
  //------------------------------------- Generate menu (p4) ----------------------------------------------------------//
  html.Append(header);
  html.Append("&lt;span  class='menu-title'&gt;INNHOLDSFORTEGNELSE&lt;/span&gt;");
  html.Append("&lt;ul class='content-list break'&gt;");
  
  //------------------ Innlednings menu --------------------------//
  if (await db.Innlednings.AnyAsync(n =&gt; n.CustomerId == customerId))
  {
     html.Append("&lt;li class='list-header'&gt;Innledning&lt;/li&gt;");
  }
  //------------------ Fordelings menu --------------------------//
  if (await db.Fordelings.AnyAsync(n =&gt; n.CustomerId == customerId))
  {
     html.Append("&lt;li class='list-header'&gt;Fordeling av bruk og regninger&lt;/li&gt;");
  }
  //--------------------------------- Report Area Bruk Menu ----------------------------------------------------//
  //--------------------------- Get transaction category id of selected area ---------------------------//
  const string tranCatBruk = "Bruk";
  var tranCatIdBruk = 0;
  if (await db.TransactionCategories.AnyAsync(n =&gt; n.Name == tranCatBruk))
  {
      tranCatIdBruk = (await (db.TransactionCategories.FirstAsync(n =&gt; n.Name == tranCatBruk))).Id;
  }
  else
  {
      throw new HttpException(404, "'" + tranCatBruk + "' transaction category does not exist!");
  }
  if (await db.ReportViews.AnyAsync(n =&gt; n.CustomerId == customerId &amp;&amp; n.TransactionCategoryId == tranCatIdBruk))
  {
     html.Append("&lt;li class='list-header'&gt;Brukskonto&lt;/li&gt;");
  
      if (await db.FordelingBruks.AnyAsync(n =&gt; n.CustomerId == customerId))
      {
         html.Append("&lt;li class='list-level2'&gt;Fordeling av brukskonto&lt;/li&gt;");
      }
  
      var rvBrukItems = await
          db.ReportViews.Where(n =&gt; n.CustomerId == customerId &amp;&amp; n.TransactionCategoryId == tranCatIdBruk).ToListAsync();
      foreach (var rvItem in rvBrukItems)
      {
          var mainCatName = (await db.Categories.FirstAsync(n =&gt; n.Id == rvItem.MainCategoryId)).Name;
  
          if (!rvItem.TotalPerMonthImage.IsNullOrWhiteSpace() ||
              !rvItem.DifferentStoresImage.IsNullOrWhiteSpace() ||
              !rvItem.SubCategoryImage.IsNullOrWhiteSpace() ||
              !rvItem.TransactionsPerMonthImage.IsNullOrWhiteSpace() ||
              !rvItem.AverageTradeValueImage.IsNullOrWhiteSpace())
          {
             html.Append("&lt;li class='list-level2'&gt;" + mainCatName + "&lt;/li&gt;");
          }
          //----------------- get images --------------------------//
          if (!rvItem.TotalPerMonthImage.IsNullOrWhiteSpace())
          {
             html.Append("&lt;li class='list-level3'&gt;Totalt pr. måned&lt;/li&gt;");
          }
          if (!rvItem.DifferentStoresImage.IsNullOrWhiteSpace())
          {
             html.Append("&lt;li class='list-level3'&gt;Fordeling&lt;/li&gt;");
          }
          if (!rvItem.SubCategoryImage.IsNullOrWhiteSpace())
          {
             html.Append("&lt;li class='list-level3'&gt;Kategorier&lt;/li&gt;");
          }
          if (!rvItem.TransactionsPerMonthImage.IsNullOrWhiteSpace())
          {
             html.Append("&lt;li class='list-level3'&gt;Transaksjoner pr. måned&lt;/li&gt;");
  
          }
          if (!rvItem.AverageTradeValueImage.IsNullOrWhiteSpace())
          {
             html.Append("&lt;li class='list-level3'&gt;Gjennomsnittlig handlebeløp&lt;/li&gt;");
  
          }
  
      }
  
  }
  //--------------------------------- Report Area Regning Menu ----------------------------------------------------//
  //--------------------------- Get transaction category id of selected area ---------------------------//
  const string tranCatRegning = "Regning";
  var tranCatIdRegning = 0;
  if (await db.TransactionCategories.AnyAsync(n =&gt; n.Name == tranCatRegning))
  {
      tranCatIdRegning = (await db.TransactionCategories.FirstAsync(n =&gt; n.Name == tranCatRegning)).Id;
  
  }
  else
  {
      throw new HttpException(404, "'" + tranCatRegning + "' transaction category does not exist!");
  }
  if (await db.ReportViews.AnyAsync(n =&gt; n.CustomerId == customerId &amp;&amp; n.TransactionCategoryId == tranCatIdRegning))
  {
     html.Append("&lt;li class='list-header'&gt;Regningskonto&lt;/li&gt;");
  
      if (await db.FordelingBruks.AnyAsync(n =&gt; n.CustomerId == customerId))
      {
         html.Append("&lt;li class='list-level2'&gt;Fordeling av regningskonto&lt;/li&gt;");
      }
  
      var rvBrukItems = await 
          db.ReportViews.Where(n =&gt; n.CustomerId == customerId &amp;&amp; n.TransactionCategoryId == tranCatIdRegning).ToListAsync();
  
      foreach (var rvItem in rvBrukItems)
      {
          var mainCatName = (await db.Categories.FirstAsync(n =&gt; n.Id == rvItem.MainCategoryId)).Name;
          if (!rvItem.TotalPerMonthImage.IsNullOrWhiteSpace() ||
              !rvItem.DifferentStoresImage.IsNullOrWhiteSpace() ||
              !rvItem.SubCategoryImage.IsNullOrWhiteSpace() ||
              !rvItem.TransactionsPerMonthImage.IsNullOrWhiteSpace() ||
              !rvItem.AverageTradeValueImage.IsNullOrWhiteSpace())
          {
             html.Append("&lt;li class='list-level2'&gt;" + mainCatName + "&lt;/li&gt;");
          }
          //----------------- get images --------------------------//
          if (!rvItem.TotalPerMonthImage.IsNullOrWhiteSpace())
          {
             html.Append("&lt;li class='list-level3'&gt;Totalt pr. måned&lt;/li&gt;");
          }
          if (!rvItem.DifferentStoresImage.IsNullOrWhiteSpace())
          {
             html.Append("&lt;li class='list-level3'&gt;Fordeling&lt;/li&gt;");
          }
          if (!rvItem.SubCategoryImage.IsNullOrWhiteSpace())
          {
             html.Append("&lt;li class='list-level3'&gt;Kategorier&lt;/li&gt;");
          }
          if (!rvItem.TransactionsPerMonthImage.IsNullOrWhiteSpace())
          {
             html.Append("&lt;li class='list-level3'&gt;Transaksjoner pr. måned&lt;/li&gt;");
  
          }
          if (!rvItem.AverageTradeValueImage.IsNullOrWhiteSpace())
          {
             html.Append("&lt;li class='list-level3'&gt;Gjennomsnittlig handlebeløp&lt;/li&gt;");
  
          }
  
      }
  
  }
  //------------------ Inntekt menu --------------------------//
  html.Append("&lt;li class='list-header'&gt;Inntekt&lt;/li&gt;");
  if (await db.InntektPerMonths.AnyAsync(n =&gt; n.CustomerId == customerId))
  {
     html.Append("&lt;li class='list-level2'&gt;Inntekt per måned&lt;/li&gt;");
  }
  if (await db.InntektSubCategories.AnyAsync(n =&gt; n.CustomerId == customerId))
  {
     html.Append("&lt;li class='list-level2'&gt;Fordeling av inntekt&lt;/li&gt;");
  }
  if (await db.InntektFordelings.AnyAsync(n =&gt; n.CustomerId == customerId))
  {
     html.Append("&lt;li class='list-level2'&gt;Fordeling mellom inntekt og forbruk&lt;/li&gt;");
  }
  
  //----------------- SPARING menu ------------------------//
  if (db.SparingKommentarers.Any(n =&gt; n.CustomerId == customerId) ||
      db.SparingTabells.Any(n =&gt; n.CustomerId == customerId))
  {
     html.Append("&lt;li class='list-header'&gt;Sparing&lt;/li&gt;");
  
      if (await db.SparingKommentarers.AnyAsync(n =&gt; n.CustomerId == customerId))
      {
         html.Append("&lt;li class='list-level2'&gt;Kommentar&lt;/li&gt;");
      }
      if (await db.SparingRads.AnyAsync(n =&gt; n.CustomerId == customerId))
      {
         html.Append("&lt;li class='list-level2'&gt;Sparetips&lt;/li&gt;");
      }
      if (await db.SparingTabells.AnyAsync(n =&gt; n.CustomerId == customerId))
      {
         html.Append("&lt;li class='list-level2'&gt;Tabell&lt;/li&gt;");
      }
  }
  //----------------- BUDSJETT menu ------------------------//
  if (db.Budsjettkommentarers.Any() ||
      db.BudsjettGraf1s.Any() ||
      db.TotalBruks.Any() ||
      db.TotalRegningers.Any())
  {
     html.Append("&lt;li class='list-header'&gt;Budsjett&lt;/li&gt;");
  
      if (await db.Budsjettkommentarers.AnyAsync(n =&gt; n.CustomerId == customerId))
      {
         html.Append("&lt;li class='list-level2'&gt;Kommentar&lt;/li&gt;");
      }
      if (await db.TotalBruks.AnyAsync(n =&gt; n.CustomerId == customerId))
      {
         html.Append("&lt;li class='list-level2'&gt;Totalt bruk&lt;/li&gt;");
      }
      if (await db.TotalRegningers.AnyAsync(n =&gt; n.CustomerId == customerId))
      {
         html.Append("&lt;li class='list-level2'&gt;Total regninger&lt;/li&gt;");
      }
      if (await db.BudsjettGraf1s.AnyAsync(n =&gt; n.CustomerId == customerId))
      {
         html.Append("&lt;li class='list-level2'&gt;Budsjett tall&lt;/li&gt;");
      }
  }
  //----------------- OPPSUMMERING(Articles) menu ------------------------//
  if (await db.Articles.AnyAsync(n =&gt; n.CustomerId == customerId))
  {
     html.Append("&lt;li class='list-header'&gt;Oppsummering&lt;/li&gt;");
  }
  
  //------------------ End of Menu -----------------------------------------------//
  html.Append("&lt;/ul&gt;");
  
  //------------------ two blank pages (6-7) ---------------------------------------//
  
  
  ////------------------------------- Start generating inner pdf ---------------------------------------------//
  ////---------------------------------------Innledning------------------------------------------------------//
  var customerforInnlending = await db.Innlednings.FirstOrDefaultAsync(inl =&gt; inl.CustomerId == customerId);
  html.Append(header);
  
  string textforInnlending = (customerforInnlending != null) ? customerforInnlending.Text : "&lt;b&gt;Empty Text&lt;/b&gt;";
  
  html.Append("&lt;h1 class='menu-title'&gt;Innledning &lt;/h1&gt;" +
          "&lt;div class='text-block'&gt;" + textforInnlending + "&lt;/div&gt;" +
          "&lt;p class='break'&gt;&lt;/p&gt;");
  html.Append("&lt;br&gt;");
  
  
  //-----------------------------------Fordeling-----------------------------------------------------------//
  var fordelingList = await db.Fordelings.Where(cus =&gt; cus.CustomerId == customerId).ToListAsync();
  foreach (var item in fordelingList)
  {
      if (!item.TransactionsImage.IsNullOrWhiteSpace())
      {
         html.Append(header);
         html.Append("&lt;h1&gt; Fordeling av bruk og regninger &lt;/h1&gt;");
         html.Append("&lt;img class='report-img circlediagram' width='800px' height='auto' src='" + item.TransactionsImage + "'&gt;&lt;/img&gt;");
         html.Append("&lt;div class='text-block'&gt;" + item.Text + "&lt;/div&gt;");
      }
      if (!item.TopTransactionsImage.IsNullOrWhiteSpace())
      {
         html.Append("&lt;img class='report-img stolpeddiagram mtop30' width='600px' height='auto' src='" + item.TopTransactionsImage + "'&gt;&lt;/img&gt;");
      }
     html.Append("&lt;div class='text-block break'&gt;&lt;/div&gt;");
  
  }
  
  //---------------------------- Bruskonto title -----------------------------------------------------------//
  imageArray = System.IO.File.ReadAllBytes(HttpContext.Server.MapPath("../Content/images/bruk_header.png"));
  base64ImageRepresentation = Convert.ToBase64String(imageArray);
  html.Append(header);
  html.Append("&lt;div class='break' style='display:block;padding-top:300px;padding-left:0px;'&gt;");
  html.Append("&lt;img class='report-img' src='data:image/png;base64," + base64ImageRepresentation + "'&gt;&lt;/img&gt;");
  html.Append("&lt;/div&gt;");
  
  
  
  ////------------------------------------------------Fordeling av Bruk------------------------------------------------------//
  var fordelingBruk = await db.FordelingBruks.Where(cus =&gt; cus.CustomerId == customerId).ToListAsync();
  foreach (var item in fordelingBruk)
  {
      if (!item.MainCatImage.IsNullOrWhiteSpace())
      {
         html.Append(header);
         html.Append("&lt;h1&gt;Fordeling av Brukskonto &lt;/h1&gt;");
         html.Append("&lt;img class='report-img circlediagram' src='" + item.MainCatImage + "'&gt;&lt;/img&gt;");
         html.Append("&lt;div class='text-block'&gt;" + item.Text + "&lt;/div&gt;");
      }
      if (!item.TopMainCatImage.IsNullOrWhiteSpace())
      {
         html.Append("&lt;img class='report-img stolpeddiagram mtop30' width='600px' height='auto' src='" + item.TopMainCatImage + "'&gt;&lt;/img&gt;");
      }
     html.Append("&lt;div class='text-block break'&gt;&lt;/div&gt;");
  }
  
  
  
  
  //------------------------------------------------ ReportView Bruk------------------------------------------------------//
  //--------------------------- Get transaction category id of selected area ---------------------------//
  var tranCat = "Bruk";
  var tranCatId = 0;
  if (await db.TransactionCategories.AnyAsync(n =&gt; n.Name == tranCat))
  {
      tranCatId = (await db.TransactionCategories.FirstAsync(n =&gt; n.Name == tranCat)).Id;
  
  }
  else
  {
      throw new HttpException(404, "'" + tranCat + "' transaction category does not exist!");
  
  }
  
  var reportViewBrukList = db.ReportViews.AsParallel().Where(n =&gt; n.TransactionCategoryId == tranCatId &amp;&amp; n.CustomerId == customerId).ToList();
  
  foreach (var rvItem in reportViewBrukList)
  {
      var mainCatName = (await db.Categories.FirstAsync(n =&gt; n.Parent == null &amp;&amp; n.Id == rvItem.MainCategoryId)).Name;
      //----------------- get images --------------------------//
      if (!rvItem.TotalPerMonthImage.IsNullOrWhiteSpace())
      {
         html.Append(header);
         html.Append("&lt;h1&gt; Diverse " + mainCatName + "&lt;/h1&gt;");
         html.Append("&lt;img class='report-img img-size' src='" + rvItem.TotalPerMonthImage + "'&gt;&lt;/img&gt;");
         html.Append("&lt;div class='text-block break'&gt;" + rvItem.TotalPerMonthText + "&lt;/div&gt;");
      }
      if (!rvItem.DifferentStoresImage.IsNullOrWhiteSpace())
      {
         html.Append(header);
         html.Append("&lt;h1&gt; Fordeling av " + mainCatName + "&lt;/h1&gt;");
         html.Append("&lt;img class='report-img img-size' src='" + rvItem.DifferentStoresImage + "'&gt;&lt;/img&gt;");
         html.Append("&lt;div class='text-block break'&gt;" + rvItem.DifferentStoresText + "&lt;/div&gt;");
  
  
      }
      if (!rvItem.SubCategoryImage.IsNullOrWhiteSpace())
      {
         html.Append(header);
         html.Append("&lt;h1&gt; Fordeling på " + mainCatName + "&lt;/h1&gt;");
         html.Append("&lt;img class='report-img img-size' src='" + rvItem.SubCategoryImage + "'&gt;&lt;/img&gt;");
         html.Append("&lt;div class='text-block break'&gt;" + rvItem.SubCategoryText + "&lt;/div&gt;");
      }
      if (!rvItem.TransactionsPerMonthImage.IsNullOrWhiteSpace())
      {
         html.Append(header);
         html.Append("&lt;h1&gt;" + mainCatName + "&lt;/h1&gt;");
         html.Append("&lt;img class='report-img img-size' src='" + rvItem.TransactionsPerMonthImage + "'&gt;&lt;/img&gt;");
         html.Append("&lt;div class='text-block break'&gt;" + rvItem.TransactionsPerMonthText + "&lt;/div&gt;");
  
      }
      if (!rvItem.AverageTradeValueImage.IsNullOrWhiteSpace())
      {
         html.Append(header);
         html.Append("&lt;h1&gt; Antall transaksjoner pr. mnd på " + mainCatName + "&lt;/h1&gt;");
         html.Append("&lt;img class='report-img img-size' src='" + rvItem.AverageTradeValueImage + "'&gt;&lt;/img&gt;");
         html.Append("&lt;div class='text-block break'&gt;" + rvItem.AverageTradeValueText + "&lt;/div&gt;");
  
      }
  
  }
  
  //---------------------------- Regningskonto title -----------------------------------------------------------//
  imageArray = System.IO.File.ReadAllBytes(HttpContext.Server.MapPath("../Content/images/regninger_header.png"));
  base64ImageRepresentation = Convert.ToBase64String(imageArray);
  html.Append(header);
  html.Append("&lt;div class='break' style='display:block;padding-top:300px;padding-left:0px;'&gt;");
  html.Append("&lt;img class='report-img' src='data:image/png;base64," + base64ImageRepresentation + "'&gt;&lt;/img&gt;");
  html.Append("&lt;/div&gt;");
  
  //-------------------------------------------- Fordeling av regninger (page 39) ---------------------------------------------------------//
  var fordelingRegninger = await db.FordelingRegningers.Where(cus =&gt; cus.CustomerId == customerId).ToListAsync();
  foreach (var item in fordelingRegninger)
  {
      if (!item.MainCatImage.IsNullOrWhiteSpace())
      {
         html.Append(header);
         html.Append("&lt;h1&gt;Fordeling av regninger &lt;/p&gt;");
         html.Append("&lt;img class='report-img circlediagram' width='800px' height='auto' src='" + item.MainCatImage + "'&gt;&lt;/img&gt;");
         html.Append("&lt;div class='text-block'&gt;" + item.Text + "&lt;/div&gt;");
      }
      if (!item.TopMainCatImage.IsNullOrWhiteSpace())
      {
         html.Append("&lt;img class='report-img stolpeddiagram mtop30' width='600px' height='auto' src='" + item.TopMainCatImage + "'&gt;&lt;/img&gt;");
      }
     html.Append("&lt;div class='text-block break'&gt;&lt;/div&gt;");
  }
  
  
  //------------------------------------------------ ReportView Regninger (p38-48)------------------------------------------------------//
  //--------------------------- Get transaction category id of selected area ---------------------------//
  tranCat = "Regning";
  tranCatId = 0;
  if (await db.TransactionCategories.AnyAsync(n =&gt; n.Name == tranCat))
  {
      tranCatId = (await db.TransactionCategories.FirstAsync(n =&gt; n.Name == tranCat)).Id;
  
  }
  else
  {
      throw new HttpException(404, "'" + tranCat + "' transaction category does not exist!");
  }
  
  var reportViewRegningerList = db.ReportViews.AsParallel().Where(n =&gt; n.TransactionCategoryId == tranCatId &amp;&amp; n.CustomerId == customerId).ToList();
  
  foreach (var rvItem in reportViewRegningerList)
  {
      var mainCatName = (await db.Categories.FirstAsync(n =&gt; n.Parent == null &amp;&amp; n.Id == rvItem.MainCategoryId)).Name;
      //----------------- get images --------------------------//
      if (!rvItem.TotalPerMonthImage.IsNullOrWhiteSpace())
      {
         html.Append(header);
         html.Append("&lt;h1&gt; Diverse " + mainCatName + "&lt;/h1&gt;");
         html.Append("&lt;img class='report-img img-size' src='" + rvItem.TotalPerMonthImage + "'&gt;&lt;/img&gt;");
         html.Append("&lt;div class='text-block break'&gt;" + rvItem.TotalPerMonthText + "&lt;/div&gt;");
      }
      if (!rvItem.DifferentStoresImage.IsNullOrWhiteSpace())
      {
         html.Append(header);
          html.Append("&lt;h1&gt; Fordeling av " + mainCatName + "&lt;/h1&gt;");
         html.Append("&lt;img class='report-img img-size' src='" + rvItem.DifferentStoresImage + "'&gt;&lt;/img&gt;");
         html.Append("&lt;div class='text-block break'&gt;" + rvItem.DifferentStoresText + "&lt;/div&gt;");
  
  
      }
      if (!rvItem.SubCategoryImage.IsNullOrWhiteSpace())
      {
         html.Append(header);
         html.Append("&lt;h1&gt; Fordeling på " + mainCatName + "&lt;/h1&gt;");
         html.Append("&lt;img class='report-img img-size' src='" + rvItem.SubCategoryImage + "'&gt;&lt;/img&gt;");
         html.Append("&lt;div class='text-block break'&gt;" + rvItem.SubCategoryText + "&lt;/div&gt;");
      }
      if (!rvItem.TransactionsPerMonthImage.IsNullOrWhiteSpace())
      {
         html.Append(header);
         html.Append("&lt;h1&gt;" + mainCatName + "&lt;/h1&gt;");
         html.Append("&lt;img class='report-img img-size' src='" + rvItem.TransactionsPerMonthImage + "'&gt;&lt;/img&gt;");
         html.Append("&lt;div class='text-block break'&gt;" + rvItem.TransactionsPerMonthText + "&lt;/div&gt;");
  
      }
      if (!rvItem.AverageTradeValueImage.IsNullOrWhiteSpace())
      {
         html.Append(header);
         html.Append("&lt;h1&gt; Antall transaksjoner pr. mnd på " + mainCatName + "&lt;/h1&gt;");
         html.Append("&lt;img class='report-img img-size' src='" + rvItem.AverageTradeValueImage + "'&gt;&lt;/img&gt;");
         html.Append("&lt;div class='text-block break'&gt;" + rvItem.AverageTradeValueText + "&lt;/div&gt;");
  
      }
  
  }
  
  
  //---------------------------- Inntekter title -----------------------------------------------------------//
  imageArray = System.IO.File.ReadAllBytes(HttpContext.Server.MapPath("../Content/images/inntekter_header.png"));
  base64ImageRepresentation = Convert.ToBase64String(imageArray);
  html.Append(header);
  html.Append("&lt;div class='break' style='display:block;padding-top:300px;padding-left:0px;'&gt;");
  html.Append("&lt;img class='report-img' src='data:image/png;base64," + base64ImageRepresentation + "'&gt;&lt;/img&gt;");
  html.Append("&lt;/div&gt;");
  
  //---------------------------------------- Inntekt per måned  (p50) --------------------------------------------------------------//
  if (await db.InntektPerMonths.AnyAsync(n =&gt; n.CustomerId == customerId))
  {
      var innPerManed = await db.InntektPerMonths.FirstAsync(n =&gt; n.CustomerId == customerId);
      if (!innPerManed.InntektPerMonthImage.IsNullOrWhiteSpace())
      {
         html.Append(header);
         html.Append("&lt;h1&gt; Inntekt per måned &lt;/h1&gt;");
         html.Append("&lt;img class='report-img img-size' src='" + innPerManed.InntektPerMonthImage + "'&gt;&lt;/img&gt;");
         html.Append("&lt;div class='text-block break'&gt;" + innPerManed.Text + "&lt;/div&gt;");
  
      }
  
  }
  
  //---------------------------------------- Fordeling av inntekt  (p51) --------------------------------------------------------------//
  if (await db.InntektFordelings.AnyAsync(n =&gt; n.CustomerId == customerId))
  {
      var innFordelings = await db.InntektSubCategories.FirstAsync(n =&gt; n.CustomerId == customerId);
      if (!innFordelings.InntektSubCategoryImage.IsNullOrWhiteSpace())
      {
         html.Append(header);
         html.Append("&lt;h1&gt; Fordeling av inntekt  &lt;/h1&gt;");
         html.Append("&lt;img class='report-img img-size' src='" + innFordelings.InntektSubCategoryImage + "'&gt;&lt;/img&gt;");
         html.Append("&lt;div class='text-block break'&gt;" + innFordelings.Text + "&lt;/div&gt;");
  
      }
  }
  
  
  //---------------------------------------- Fordeling mellom inntekt og forbruk   (p52) --------------------------------------------------------------//
  if (await db.InntektSubCategories.AnyAsync(n =&gt; n.CustomerId == customerId))
  {
      var innSub = await db.InntektFordelings.FirstAsync(n =&gt; n.CustomerId == customerId);
      if (!innSub.InntektFordelingImage.IsNullOrWhiteSpace())
      {
         html.Append(header);
         html.Append("&lt;h1&gt; Fordeling mellom inntekt og forbruk  &lt;/h1&gt;");
         html.Append("&lt;img class='report-img img-size' src='" + innSub.InntektFordelingImage + "'&gt;&lt;/img&gt;");
         html.Append("&lt;div class='text-block break'&gt;" + innSub.Text + "&lt;/div&gt;");
  
      }
  }
  
  html.Append("&lt;/body&gt;&lt;/html&gt;");
  
  //---------- some code 
  //........
  
  var htmlStr = html.toString();
  </code></pre>