c#将值添加到现有列表<class> </class>

时间:2014-03-31 21:41:39

标签: c# linq list

试图通过.NET文档找到解决方案,但尚未真正获得或找到答案。

所以我定义了一个类:

  public class Hold
  {
      public DateTime Time  { get; set; }
      public Double PosX    { get; set; }
      public Double PosY    { get; set; }
      public Double avgTime { get; set; }
  }

然后我将我的列表定义为List<Hold> data;

并有一个时间列表,例如List<DateTime> time;

我通过linq for循环获取3个字段Time,PosX和PosY的值

接下来我运行这个

foreach(Hold h in data)
{
    time.add(h.Time); //Add to the List so we can look at the Time Field Further

    for(int i =0; i < time.Count; i++)
    {
        double avg // Variable works out the time between Time Records in a Database their is some assignment code but sure if it is relevant to the question ?
        data.add({ avgTime = avg}); <-- problem here as I want to add avg to my Data class List in the field of avgTime
    }
}

1 个答案:

答案 0 :(得分:0)

我认为您要设置h.avgTime的值,因为h是您正在处理的当前元素。

foreach(Hold h in data)
{
    time.add(h.Time);

    for(int i =0; i < time.Count; i++)
    {
        double avg = 0; //Whatever calculation you want
        h.avgTime = avg;
    }
}