views:

67

answers:

2

How can i solve out of memory exception in list generic if adding new value

  foreach(DataColumn dc in dTable.Columns)
                foreach (DataRow dr in dTable.Rows)
                    myScriptCellsCount.MyCellsCharactersCount.Add(dr[dc].ToString().Length);

MyBase Class:

    public class MyExcelSheetsCells
    {
        public List<int> MyCellsCharactersCount { get; set; }

        public MyExcelSheetsCells()
        {
            MyCellsCharactersCount = new List<int>();
        }

    }
+2  A: 

Use fewer rows and columns. Right now you're creating a new entry in MyCellsCharacterCount for the number of rows times the number of columns, which could pretty easily exceed the amount of available memory if you have a lot of data.

JSBangs
A: 

If your table is simply too large for your column * row data, perhaps performing whatever meaningful operation/aggregation you're after (avg, sum, min, max, etc) would be better than simply saving off the counts individually.

Essentially, get a summary view instead of a detail view.

By your example, I have no way of guesstimating what you're after as what you're showing is simply storing the column-major count of characters.

JS Bangs' answer is giving you the reason, but neither of us can really give you an answer to your question without more information.

Marc