简体   繁体   中英

How do I sum a dynamic list in C#?

Using the below block of code, how can I write a sum formula when the number of rows may change each time? I have it hardcoded to sum C11:C33 and D11:D33 but I'm trying to move away from that. I know I can call the first and last values and use that as a range, but I'm struggling with the syntax.
Very new to this so apologies/thanks in advance!

using (FulfillmentEntities DB3 = new FulfillmentEntities())
                            { 
                                TotalRows = results5.Count();

                                foreach (var d in results5)
                                {
                                    int col = 2;

                                    worksheet.Cells[row, col++].Value = d.Category;
                                    worksheet.Cells[row, col++].Value = d.intA;
                                    worksheet.Cells[row, col++].Value = d.intB;

                                    row++;

                                }
                                int TotalCol = 2;

                                worksheet.Cells[row, TotalCol++].Value =  "Total";
                                worksheet.Cells[row, TotalCol++].Formula = "SUM(C11:C33)";
                                worksheet.Cells[row, TotalCol++].Formula = "SUM(D11:D33)";
                            }

Simply pass the list to the GetRowMinAndMax method. This method assumes that the list is not null or empty and that youre looking for a range in a single column

public Tuple<string,string> GetRowMinAndMax(List<string> colRows)
{
   var rowLow = colRows.Min(ExtractRow);
    var rowMax = colRows.Max(ExtractRow);
    var col = ExtractCol(colRows.First());
    return Tuple.Create(col + rowLow, col + rowMax);
}

public int ExtractRow(string colRow)
{
  return int.Parse(new string(colRow.Where(char.IsDigit).ToArray()));
}

public string ExtractCol(string colRow)
{
    return new string(colRow.Where(char.IsLetter).ToArray());
}

EDIT

Actually this all probably an over complication since its not across columns. The reason why list.First() doesn't work is because the list is not ordered so you're taking out the first element you put in. You could prob just order the list by calling

var orderedList = list.orderBy(s=> s);

then first and last would work.

Just saw your edit Alex, thanks for all your help. The tuple solution was a little over my head. Below is what I wound up doing that worked perfectly and was very concise. Created a variable for what I wanted my total to be, and incremented it with each row iteration using the += operator. Then just called it outside of my foreach loop.

                           int? totalClientURL = 0;   
                           int? totalHgURL = 0;
                                foreach (var d in results5)
                                {
                                    int col = 6;

                                    worksheet.Cells[row, col++].Value = d.employees_range;
                                    worksheet.Cells[row, col].StyleName = styleData;
                                    worksheet.Cells[row, col++].Value = d.client_url_count;
                                    totalClientURL += d.client_url_count;
                                    worksheet.Cells[row, col].StyleName = styleData;
                                    worksheet.Cells[row, col++].Value = d.url_count;
                                    totalHgURL += d.url_count;


                                    row++;

                                    ExportCount++;
                                    if (ExportCount % 100 == 0)
                                        Worker.ReportProgress(20, ExportCount + " rows exported of " + TotalRows);
                                }

                                int TotalCol = 6;
                                worksheet.Cells[row, TotalCol++].Value = "Total";
                                worksheet.Cells[row, TotalCol].StyleName = styleData;
                                worksheet.Cells[row, TotalCol++].Value = totalClientURL;
                                worksheet.Cells[row, TotalCol].StyleName = styleData;
                                worksheet.Cells[row, TotalCol++].Value = totalHgURL;                                 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM