Reputation:
I want to access a certain field within a Class List so that I can do a calculation within a loop.
Here's my Class Firstly:
public class Hold
{
public DateTime Time { get; set; }
public Double XPosition { get; set; }
public Double YPosition { get; set; }
}
then my list List<Hold>Data;
I then use Linq to Store values from a Query.
What I ideally want to do is access the Time part of the List like so:
for(int i =0; i < Data.Count; i++)
{
double average = Data.Time // etc etc
}
Upvotes: 1
Views: 9854
Reputation: 9506
//get all values of Time Property from Data collection
List<DateTime> dateList = new List<DateTime>();
Data.ForEach((h)=> { dateList.Add(h.Time); });
Upvotes: 0
Reputation: 216363
Looking for a simple foreach?
foreach(Hold h in Data)
{
DateTime t = h.Time;
... other properties or whatever method defined for the class Hold
}
A List<T>
implements the IEnumerator interface, so it is natural to use foreach when you need to loop over all the elements of the List and extract its objects
The only possible problem with the foreach approach is if you need to remove elements from the list while iterating over them. But if you don't need this, then foreach is the most simple and natural way to access the elements contained in the list.
However it would be interesting to know what you want to do inside the loop.
Perhaps there are simpler ways using Linq and completely remove the loop.
For example, if you want to calculate the average seconds of your Time values stored in the list you could remove the loop using
var k = data.Select(x => new TimeSpan(x.Time.Hour, x.Time.Minute, x.Time.Second))
.Average(z => z.TotalSeconds);
Upvotes: 1
Reputation: 148180
You need to use indexer Data[i]
with the list object. You have data type of DateTime and should not be assigned to double directly.
for(int i =0; i < Data.Count; i++)
{
//double average = Data[i].Time;
//Your code goes here.
}
You can use foreach loop
for(var data in Data)
{
//data.Time //You can use data.Time here
}
Upvotes: 1
Reputation: 56727
The list does not have a "Time part". The objects held by the list have a Time
member.
You could do this:
for(int i =0; i < Data.Count; i++)
{
double average = Data[i].Time etc etc
}
To make Saverio Terracciano happy:
for (int i = 0; i < Data.Count; i++)
{
// Access members of Hold, write something like
... = Data[i].<Member Name>
}
If you don't evaluate the i
in any other place than just for indexing the list, you could also write
foreach (Hold h in Data)
{
... = h.<Member Name>;
}
Upvotes: 1
Reputation: 3915
If I understand correctly (from your variable name) you want to calculate the average over your datetime values. You should do something like this:
double totalTicks=0D;
foreach(var x in Data){
totalTicks+=x.Time.Ticks;
}
double averageTicks=totalTicks / Data.Count();
DateTime avgDateTime=new DateTime(averageTicks);
Upvotes: 0
Reputation: 77364
To create the average of a member of a list you need to take two steps:
You need to transform the list of class instances to a list of values you want to do your calculation on. You can do this using the Select
extension method.
You need to calculate the average. You can do this using the Average
extension method.
var data = new List<Hold>();
// fill list
var transformed = data.Select(hold => hold.Time);
var average = transformed.Average();
You can do this in one simple step by using an overload of the Average extension method that includes a transform:
var average = data.Average(hold => hold.Time);
Upvotes: 1