0

我正在研究一种生成pdf的方法。为此,我正在使用 tuespechkin library 。转换本身非常快。大多数时候,它需要从数据库中检索数据并创建 pdf 的 html 表示。为了提高性能,我尝试使用异步方法和 Plinq,但这并没有太大帮助。有没有办法优化这种方法?

代码中最昂贵的部分(更新):

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 ----------------------------------------------------//
//--------------------------- Get 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 does 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>");
        }
        //----------------- get 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 ----------------------------------------------------//
//--------------------------- Get 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 does 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>");
        }
        //----------------- get 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>");

//------------------ two 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------------------------------------------------------//
//--------------------------- Get 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 does 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;
    //----------------- get 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)------------------------------------------------------//
//--------------------------- Get 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 does 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;
    //----------------- get 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>");

//---------- some code 
//........

var htmlStr = html.toString();
4

2 回答 2

0

字符串在 .NET 中是不可变的。您应该使用 aStringBuilder而不是字符串。

像这样的东西:

StringBuilder sb = new StringBuilder(16 * 1024); // adapt memory reservation depending on document size
sb.Append("string_to_append");
...
return sb.ToString();

这种技术比+=

于 2015-06-03T08:50:48.547 回答
0

您可以执行以下操作来提高性能:

  1. 尽量减少在 StringBuilder 实例上使用 Append() 并以链式方式调用它。这可能会导致更多调用 append 方法。但这取决于。您需要检查并决定什么是最适合您的。
  2. 替换var为除类型之外的确切anonymous类型。
  3. 不要使用async/await(删除并观察它是否会影响性能),因为我认为这对您的情况没有好处。(这是可选的)
  4. 将变量置于foreach循环之外。

例子 :

html.Append("<div class='break' style='margin: 0 0 0 0px;'><img class='report-img' style='margin:150px 0 400px 0;' src='data:image/jpeg;base64,")
                .Append(base64ImageRepresentation)
                .Append("</img><p class='purpleText' style='color:#8E0033; font-size: 30px; margin:0; line-height:20px;'>")
                .Append( title)
                .Append("</p><p class='purpleText' style='color:#8E0033; font-size: 16px;'>")
                .Append( bigTitle)
                .Append("</p><p style='color:#980000; font-size: 50px; margin: 46px 0 0 0;'>")
                .Append(custName.firstName)
                .Append("<span>")
                .Append(custName.lastName)
                .Append("</span></p></div>");

示例varvar tranCatIdBruk = 0;int tranCatIdBruk = 0;

您在 foreach 循环中使用了 var 关键字,这将导致性能下降(相信我,我已经做到了,当时我很震惊:D)

外部变量示例foreach

var mainCatName; // Replace var with string.
foreach (var rvItem in rvBrukItems)
    {
       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>");
        }
}

StringBuilder Append()你可以参考下面的链接,它解释了内部发生的事情:

字符串生成器最佳实践

希望这会帮助你。如果您还有其他疑问,请告诉我。

于 2015-06-04T06:26:18.877 回答