c#add to list <datecount> - DateTime Best way </datecount>

时间:2013-12-05 11:04:34

标签: c# list collections

我有一个日期列表(目前在所选结果中有4000个) 我试图将结果放在图表中,

班级看起来像这样

public class DisplayObjectDates
{
    public int Month { get; set; }
    public int Year { get; set; }
    public int day { get; set; }
    public DateTime fulldatetime { get; set; }
    public int CountedDate { get; set; }
}

我有一个班级列表

private static List<DisplayObjectDates> SortedDatesDays = new List<DisplayObjectDates>();

我在从EF调用并获得返回列表

后添加到列表中
if (SortedDatesDays.Count() == 0)
                {
                    var addDisplayObjectDatesYear = new DisplayObjectDates();
                    addDisplayObjectDatesYear.Year = contextreturned.change_time.Year;
                    addDisplayObjectDatesYear.Month = contextreturned.change_time.Month;
                    addDisplayObjectDatesYear.day = contextreturned.change_time.Day;
                    addDisplayObjectDatesYear.fulldatetime = contextreturned.change_time;
                    addDisplayObjectDatesYear.CountedDate = 1;
                    SortedDatesDays.Add(addDisplayObjectDatesYear);
                }
                else
                {
                    foreach (var VARIABLE in SortedDatesDays)
                    {
                        if (VARIABLE.day == contextreturned.change_time.Day && VARIABLE.Month == contextreturned.change_time.Month && VARIABLE.Year == contextreturned.change_time.Year)
                        {
                            VARIABLE.CountedDate = VARIABLE.CountedDate++;
                        }
                        else
                        {
                            var addDisplayObjectDatesYear = new DisplayObjectDates();
                            addDisplayObjectDatesYear.Year = contextreturned.change_time.Year;
                            addDisplayObjectDatesYear.Month = contextreturned.change_time.Month;
                            addDisplayObjectDatesYear.day = contextreturned.change_time.Day;
                            addDisplayObjectDatesYear.fulldatetime = contextreturned.change_time;
                            addDisplayObjectDatesYear.CountedDate = 1;
                            SortedDatesDays.Add(addDisplayObjectDatesYear);
                        }
                    }
                }

这给了我一个错误 收集被修改;枚举操作可能无法执行。

所以我改变了

 foreach (var VARIABLE in SortedDatesDays)

foreach (var VARIABLE in SortedDatesDays.ToList())

现在我内存不足

在一天结束时,我需要计算在某个特定日期发生的事件的次数 我需要把它放到一个我可以在图表中使用的表格中(DEVEXPRESS) 我不确定我是否应该使用linq或当前系统。 所有想法的任何帮助与这样做的正确方式将不胜感激 谢谢 a0011010011

2 个答案:

答案 0 :(得分:1)

我实际上认为foreach (var VARIABLE in SortedDatesDays.ToArray())会解决问题,因为我经常使用它。

在这种情况下,请尝试以下操作。

...
else
{
    // create a temporary collection for storing new items
    var list = new List<DisplayObjectDates>();

    foreach (var VARIABLE in SortedDatesDays)
    {
         if (...) { ... }
         else
         {
              var addDisplayObjectDatesYear = new DisplayObjectDates();
              ...
              // place it to the new list instead
              list.Add(addDisplayObjectDatesYear);
         }
     }

     // merge lists
     SortedDatesDays.AddRange(list);
}

答案 1 :(得分:0)

虽然你正在分配 FullDateTime DisplayObjectDates对象...我更喜欢说你重建你的课程可能是这样的...它也有助于你的程序使用较少的memery ..

public class DisplayObjectDates
    {
        public int Month { get { return fulldatetime.Month; } }
        public int Year { get { return fulldatetime.Year; } }
        public int day { get { return fulldatetime.Day; } }
        public DateTime fulldatetime { get; set; }
        public int CountedDate { get; set; }
    }

然后对初始化函数进行排序

if (SortedDatesDays.Count() == 0)
        {
            var addDisplayObjectDatesYear = new DisplayObjectDates();                 
            addDisplayObjectDatesYear.fulldatetime = contextreturned.change_time;
            addDisplayObjectDatesYear.CountedDate = 1;
            SortedDatesDays.Add(addDisplayObjectDatesYear);
        }
        else
        {
            foreach (var VARIABLE in SortedDatesDays)
            {
                if (VARIABLE.fulldatetime.Date == contextreturned.change_time.fulldatetime.Date)
                {
                    VARIABLE.CountedDate = VARIABLE.CountedDate++;
                }
                else
                {
                    var addDisplayObjectDatesYear = new DisplayObjectDates();
                    addDisplayObjectDatesYear.fulldatetime = contextreturned.change_time;
                    addDisplayObjectDatesYear.CountedDate = 1;
                    SortedDatesDays.Add(addDisplayObjectDatesYear);
                }
            }
        }