Linq Skip and Take on Class List
up vote
0
down vote
favorite
As according to this stack question: How to calculate the rolling average of a C# array list?
With a normal list you can take a running average of values in the list like this:
List<int> numlist = new List<int>();
List<double> averages = Enumerable.Range(0, numlist.Count - 3).
Select(i => numlist.Skip(i).Take(4).Average()).
ToList();
I want to do the same, however on a specific subset of a class list:
public class Data_Raw
{
public DateTime DateDataRaw { set; get; }
public double PriceRaw { set; get; }
public double PercentageReturnsRaw { set; get; }
public Data_Raw(DateTime _dateDataRaw,double _priceRaw, double _percentageReturnsRaw)
{
DateDataRaw = _dateDataRaw; //1
PriceRaw = _priceRaw;
PercentageReturnsRaw = _percentageReturnsRaw;
}
}
List<Data_Raw> myRawData = new List<Data_Raw>();
So now I want to get the average, for instance of the PriceRaw double data, within the class list which is myRawData. Please help
EDIT:
If these are the data for the PriceRaw variable of the myRawData list:
myRawData[0].PriceRaw=2
myRawData[1].PriceRaw=3
myRawData[2].PriceRaw=4
myRawData[3].PriceRaw=5
How do I get the average value of the last three PriceRaw values which is = (3+4+5)/3 = 4. It is only available directly after myRawData.Skip
Skip is not available if I try to append it to myRawData.PriceRaw.Skip
This stack question gets me closer, but can't figure it out: Linq query a List of objects containing a list of object
c# linq average
add a comment |
up vote
0
down vote
favorite
As according to this stack question: How to calculate the rolling average of a C# array list?
With a normal list you can take a running average of values in the list like this:
List<int> numlist = new List<int>();
List<double> averages = Enumerable.Range(0, numlist.Count - 3).
Select(i => numlist.Skip(i).Take(4).Average()).
ToList();
I want to do the same, however on a specific subset of a class list:
public class Data_Raw
{
public DateTime DateDataRaw { set; get; }
public double PriceRaw { set; get; }
public double PercentageReturnsRaw { set; get; }
public Data_Raw(DateTime _dateDataRaw,double _priceRaw, double _percentageReturnsRaw)
{
DateDataRaw = _dateDataRaw; //1
PriceRaw = _priceRaw;
PercentageReturnsRaw = _percentageReturnsRaw;
}
}
List<Data_Raw> myRawData = new List<Data_Raw>();
So now I want to get the average, for instance of the PriceRaw double data, within the class list which is myRawData. Please help
EDIT:
If these are the data for the PriceRaw variable of the myRawData list:
myRawData[0].PriceRaw=2
myRawData[1].PriceRaw=3
myRawData[2].PriceRaw=4
myRawData[3].PriceRaw=5
How do I get the average value of the last three PriceRaw values which is = (3+4+5)/3 = 4. It is only available directly after myRawData.Skip
Skip is not available if I try to append it to myRawData.PriceRaw.Skip
This stack question gets me closer, but can't figure it out: Linq query a List of objects containing a list of object
c# linq average
3
So what is the issue? Why don't you do it in the way you talked about at the top?
– rory.ap
Nov 7 at 12:18
It would be awesome if you could provide a Minimal, Complete, and Verifiable example including sample data, and specify the exact results you are wanting to achieve.
– mjwills
Nov 7 at 12:57
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
As according to this stack question: How to calculate the rolling average of a C# array list?
With a normal list you can take a running average of values in the list like this:
List<int> numlist = new List<int>();
List<double> averages = Enumerable.Range(0, numlist.Count - 3).
Select(i => numlist.Skip(i).Take(4).Average()).
ToList();
I want to do the same, however on a specific subset of a class list:
public class Data_Raw
{
public DateTime DateDataRaw { set; get; }
public double PriceRaw { set; get; }
public double PercentageReturnsRaw { set; get; }
public Data_Raw(DateTime _dateDataRaw,double _priceRaw, double _percentageReturnsRaw)
{
DateDataRaw = _dateDataRaw; //1
PriceRaw = _priceRaw;
PercentageReturnsRaw = _percentageReturnsRaw;
}
}
List<Data_Raw> myRawData = new List<Data_Raw>();
So now I want to get the average, for instance of the PriceRaw double data, within the class list which is myRawData. Please help
EDIT:
If these are the data for the PriceRaw variable of the myRawData list:
myRawData[0].PriceRaw=2
myRawData[1].PriceRaw=3
myRawData[2].PriceRaw=4
myRawData[3].PriceRaw=5
How do I get the average value of the last three PriceRaw values which is = (3+4+5)/3 = 4. It is only available directly after myRawData.Skip
Skip is not available if I try to append it to myRawData.PriceRaw.Skip
This stack question gets me closer, but can't figure it out: Linq query a List of objects containing a list of object
c# linq average
As according to this stack question: How to calculate the rolling average of a C# array list?
With a normal list you can take a running average of values in the list like this:
List<int> numlist = new List<int>();
List<double> averages = Enumerable.Range(0, numlist.Count - 3).
Select(i => numlist.Skip(i).Take(4).Average()).
ToList();
I want to do the same, however on a specific subset of a class list:
public class Data_Raw
{
public DateTime DateDataRaw { set; get; }
public double PriceRaw { set; get; }
public double PercentageReturnsRaw { set; get; }
public Data_Raw(DateTime _dateDataRaw,double _priceRaw, double _percentageReturnsRaw)
{
DateDataRaw = _dateDataRaw; //1
PriceRaw = _priceRaw;
PercentageReturnsRaw = _percentageReturnsRaw;
}
}
List<Data_Raw> myRawData = new List<Data_Raw>();
So now I want to get the average, for instance of the PriceRaw double data, within the class list which is myRawData. Please help
EDIT:
If these are the data for the PriceRaw variable of the myRawData list:
myRawData[0].PriceRaw=2
myRawData[1].PriceRaw=3
myRawData[2].PriceRaw=4
myRawData[3].PriceRaw=5
How do I get the average value of the last three PriceRaw values which is = (3+4+5)/3 = 4. It is only available directly after myRawData.Skip
Skip is not available if I try to append it to myRawData.PriceRaw.Skip
This stack question gets me closer, but can't figure it out: Linq query a List of objects containing a list of object
c# linq average
c# linq average
edited Nov 8 at 13:44
asked Nov 7 at 12:16
Allstar
67113
67113
3
So what is the issue? Why don't you do it in the way you talked about at the top?
– rory.ap
Nov 7 at 12:18
It would be awesome if you could provide a Minimal, Complete, and Verifiable example including sample data, and specify the exact results you are wanting to achieve.
– mjwills
Nov 7 at 12:57
add a comment |
3
So what is the issue? Why don't you do it in the way you talked about at the top?
– rory.ap
Nov 7 at 12:18
It would be awesome if you could provide a Minimal, Complete, and Verifiable example including sample data, and specify the exact results you are wanting to achieve.
– mjwills
Nov 7 at 12:57
3
3
So what is the issue? Why don't you do it in the way you talked about at the top?
– rory.ap
Nov 7 at 12:18
So what is the issue? Why don't you do it in the way you talked about at the top?
– rory.ap
Nov 7 at 12:18
It would be awesome if you could provide a Minimal, Complete, and Verifiable example including sample data, and specify the exact results you are wanting to achieve.
– mjwills
Nov 7 at 12:57
It would be awesome if you could provide a Minimal, Complete, and Verifiable example including sample data, and specify the exact results you are wanting to achieve.
– mjwills
Nov 7 at 12:57
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
Simply select the value you need into your numList
(see first line):
var numList = myRawData.Select(x => x.PriceRaw).ToList();
List<double> averages = Enumerable.Range(0, numlist.Count - 3).
Select(i => numlist.Skip(i).Take(4).Average()).
ToList();
An alternate way could be this:
List<double> averages = Enumerable.Range(0, myRawData.Count - 3).
Select(i => myRawData.Skip(i).Take(4).Select(x => x.PriceRaw).Average()).
ToList();
thanks I will see to read a tutorial, however your example does not work..
– Allstar
Nov 7 at 14:08
Sorry, fixed that
– Daniel Hilgarth
Nov 7 at 16:29
that just defeats the purpose of having a class list that holds all your data, vs several normal lists... it breaks the class list down to access the data
– Allstar
Nov 8 at 12:58
See update for an alternative
– Daniel Hilgarth
Nov 8 at 16:13
add a comment |
up vote
0
down vote
I think it would be easier for you to manage it if you use comprehension syntax. ie:
void Main()
{
List<Data_Raw> myRawData = new List<Data_Raw> {
new Data_Raw(new DateTime(2018,1,1), 2, 1),
new Data_Raw(new DateTime(2018,1,2), 3, 1),
new Data_Raw(new DateTime(2018,1,3), 4, 1),
new Data_Raw(new DateTime(2018,1,4), 5, 1),
new Data_Raw(new DateTime(2018,1,5), 6, 1),
new Data_Raw(new DateTime(2018,1,6), 7, 1),
new Data_Raw(new DateTime(2018,1,7), 8, 1),
};
var groupSize = 3;
var result = from i in Enumerable.Range(0, myRawData.Count() - (groupSize - 1))
let myGroup = myRawData.Skip(i).Take(groupSize)
select new
{
Values = string.Join(",", myGroup.Select(rd => rd.PriceRaw.ToString())),
Average = myGroup.Average(rd => rd.PriceRaw)
};
}
public class Data_Raw
{
public DateTime DateDataRaw { set; get; }
public double PriceRaw { set; get; }
public double PercentageReturnsRaw { set; get; }
public Data_Raw(DateTime _dateDataRaw, double _priceRaw, double _percentageReturnsRaw)
{
DateDataRaw = _dateDataRaw; //1
PriceRaw = _priceRaw;
PercentageReturnsRaw = _percentageReturnsRaw;
}
}
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Simply select the value you need into your numList
(see first line):
var numList = myRawData.Select(x => x.PriceRaw).ToList();
List<double> averages = Enumerable.Range(0, numlist.Count - 3).
Select(i => numlist.Skip(i).Take(4).Average()).
ToList();
An alternate way could be this:
List<double> averages = Enumerable.Range(0, myRawData.Count - 3).
Select(i => myRawData.Skip(i).Take(4).Select(x => x.PriceRaw).Average()).
ToList();
thanks I will see to read a tutorial, however your example does not work..
– Allstar
Nov 7 at 14:08
Sorry, fixed that
– Daniel Hilgarth
Nov 7 at 16:29
that just defeats the purpose of having a class list that holds all your data, vs several normal lists... it breaks the class list down to access the data
– Allstar
Nov 8 at 12:58
See update for an alternative
– Daniel Hilgarth
Nov 8 at 16:13
add a comment |
up vote
0
down vote
accepted
Simply select the value you need into your numList
(see first line):
var numList = myRawData.Select(x => x.PriceRaw).ToList();
List<double> averages = Enumerable.Range(0, numlist.Count - 3).
Select(i => numlist.Skip(i).Take(4).Average()).
ToList();
An alternate way could be this:
List<double> averages = Enumerable.Range(0, myRawData.Count - 3).
Select(i => myRawData.Skip(i).Take(4).Select(x => x.PriceRaw).Average()).
ToList();
thanks I will see to read a tutorial, however your example does not work..
– Allstar
Nov 7 at 14:08
Sorry, fixed that
– Daniel Hilgarth
Nov 7 at 16:29
that just defeats the purpose of having a class list that holds all your data, vs several normal lists... it breaks the class list down to access the data
– Allstar
Nov 8 at 12:58
See update for an alternative
– Daniel Hilgarth
Nov 8 at 16:13
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Simply select the value you need into your numList
(see first line):
var numList = myRawData.Select(x => x.PriceRaw).ToList();
List<double> averages = Enumerable.Range(0, numlist.Count - 3).
Select(i => numlist.Skip(i).Take(4).Average()).
ToList();
An alternate way could be this:
List<double> averages = Enumerable.Range(0, myRawData.Count - 3).
Select(i => myRawData.Skip(i).Take(4).Select(x => x.PriceRaw).Average()).
ToList();
Simply select the value you need into your numList
(see first line):
var numList = myRawData.Select(x => x.PriceRaw).ToList();
List<double> averages = Enumerable.Range(0, numlist.Count - 3).
Select(i => numlist.Skip(i).Take(4).Average()).
ToList();
An alternate way could be this:
List<double> averages = Enumerable.Range(0, myRawData.Count - 3).
Select(i => myRawData.Skip(i).Take(4).Select(x => x.PriceRaw).Average()).
ToList();
edited Nov 8 at 16:19
answered Nov 7 at 12:19
Daniel Hilgarth
137k32245353
137k32245353
thanks I will see to read a tutorial, however your example does not work..
– Allstar
Nov 7 at 14:08
Sorry, fixed that
– Daniel Hilgarth
Nov 7 at 16:29
that just defeats the purpose of having a class list that holds all your data, vs several normal lists... it breaks the class list down to access the data
– Allstar
Nov 8 at 12:58
See update for an alternative
– Daniel Hilgarth
Nov 8 at 16:13
add a comment |
thanks I will see to read a tutorial, however your example does not work..
– Allstar
Nov 7 at 14:08
Sorry, fixed that
– Daniel Hilgarth
Nov 7 at 16:29
that just defeats the purpose of having a class list that holds all your data, vs several normal lists... it breaks the class list down to access the data
– Allstar
Nov 8 at 12:58
See update for an alternative
– Daniel Hilgarth
Nov 8 at 16:13
thanks I will see to read a tutorial, however your example does not work..
– Allstar
Nov 7 at 14:08
thanks I will see to read a tutorial, however your example does not work..
– Allstar
Nov 7 at 14:08
Sorry, fixed that
– Daniel Hilgarth
Nov 7 at 16:29
Sorry, fixed that
– Daniel Hilgarth
Nov 7 at 16:29
that just defeats the purpose of having a class list that holds all your data, vs several normal lists... it breaks the class list down to access the data
– Allstar
Nov 8 at 12:58
that just defeats the purpose of having a class list that holds all your data, vs several normal lists... it breaks the class list down to access the data
– Allstar
Nov 8 at 12:58
See update for an alternative
– Daniel Hilgarth
Nov 8 at 16:13
See update for an alternative
– Daniel Hilgarth
Nov 8 at 16:13
add a comment |
up vote
0
down vote
I think it would be easier for you to manage it if you use comprehension syntax. ie:
void Main()
{
List<Data_Raw> myRawData = new List<Data_Raw> {
new Data_Raw(new DateTime(2018,1,1), 2, 1),
new Data_Raw(new DateTime(2018,1,2), 3, 1),
new Data_Raw(new DateTime(2018,1,3), 4, 1),
new Data_Raw(new DateTime(2018,1,4), 5, 1),
new Data_Raw(new DateTime(2018,1,5), 6, 1),
new Data_Raw(new DateTime(2018,1,6), 7, 1),
new Data_Raw(new DateTime(2018,1,7), 8, 1),
};
var groupSize = 3;
var result = from i in Enumerable.Range(0, myRawData.Count() - (groupSize - 1))
let myGroup = myRawData.Skip(i).Take(groupSize)
select new
{
Values = string.Join(",", myGroup.Select(rd => rd.PriceRaw.ToString())),
Average = myGroup.Average(rd => rd.PriceRaw)
};
}
public class Data_Raw
{
public DateTime DateDataRaw { set; get; }
public double PriceRaw { set; get; }
public double PercentageReturnsRaw { set; get; }
public Data_Raw(DateTime _dateDataRaw, double _priceRaw, double _percentageReturnsRaw)
{
DateDataRaw = _dateDataRaw; //1
PriceRaw = _priceRaw;
PercentageReturnsRaw = _percentageReturnsRaw;
}
}
add a comment |
up vote
0
down vote
I think it would be easier for you to manage it if you use comprehension syntax. ie:
void Main()
{
List<Data_Raw> myRawData = new List<Data_Raw> {
new Data_Raw(new DateTime(2018,1,1), 2, 1),
new Data_Raw(new DateTime(2018,1,2), 3, 1),
new Data_Raw(new DateTime(2018,1,3), 4, 1),
new Data_Raw(new DateTime(2018,1,4), 5, 1),
new Data_Raw(new DateTime(2018,1,5), 6, 1),
new Data_Raw(new DateTime(2018,1,6), 7, 1),
new Data_Raw(new DateTime(2018,1,7), 8, 1),
};
var groupSize = 3;
var result = from i in Enumerable.Range(0, myRawData.Count() - (groupSize - 1))
let myGroup = myRawData.Skip(i).Take(groupSize)
select new
{
Values = string.Join(",", myGroup.Select(rd => rd.PriceRaw.ToString())),
Average = myGroup.Average(rd => rd.PriceRaw)
};
}
public class Data_Raw
{
public DateTime DateDataRaw { set; get; }
public double PriceRaw { set; get; }
public double PercentageReturnsRaw { set; get; }
public Data_Raw(DateTime _dateDataRaw, double _priceRaw, double _percentageReturnsRaw)
{
DateDataRaw = _dateDataRaw; //1
PriceRaw = _priceRaw;
PercentageReturnsRaw = _percentageReturnsRaw;
}
}
add a comment |
up vote
0
down vote
up vote
0
down vote
I think it would be easier for you to manage it if you use comprehension syntax. ie:
void Main()
{
List<Data_Raw> myRawData = new List<Data_Raw> {
new Data_Raw(new DateTime(2018,1,1), 2, 1),
new Data_Raw(new DateTime(2018,1,2), 3, 1),
new Data_Raw(new DateTime(2018,1,3), 4, 1),
new Data_Raw(new DateTime(2018,1,4), 5, 1),
new Data_Raw(new DateTime(2018,1,5), 6, 1),
new Data_Raw(new DateTime(2018,1,6), 7, 1),
new Data_Raw(new DateTime(2018,1,7), 8, 1),
};
var groupSize = 3;
var result = from i in Enumerable.Range(0, myRawData.Count() - (groupSize - 1))
let myGroup = myRawData.Skip(i).Take(groupSize)
select new
{
Values = string.Join(",", myGroup.Select(rd => rd.PriceRaw.ToString())),
Average = myGroup.Average(rd => rd.PriceRaw)
};
}
public class Data_Raw
{
public DateTime DateDataRaw { set; get; }
public double PriceRaw { set; get; }
public double PercentageReturnsRaw { set; get; }
public Data_Raw(DateTime _dateDataRaw, double _priceRaw, double _percentageReturnsRaw)
{
DateDataRaw = _dateDataRaw; //1
PriceRaw = _priceRaw;
PercentageReturnsRaw = _percentageReturnsRaw;
}
}
I think it would be easier for you to manage it if you use comprehension syntax. ie:
void Main()
{
List<Data_Raw> myRawData = new List<Data_Raw> {
new Data_Raw(new DateTime(2018,1,1), 2, 1),
new Data_Raw(new DateTime(2018,1,2), 3, 1),
new Data_Raw(new DateTime(2018,1,3), 4, 1),
new Data_Raw(new DateTime(2018,1,4), 5, 1),
new Data_Raw(new DateTime(2018,1,5), 6, 1),
new Data_Raw(new DateTime(2018,1,6), 7, 1),
new Data_Raw(new DateTime(2018,1,7), 8, 1),
};
var groupSize = 3;
var result = from i in Enumerable.Range(0, myRawData.Count() - (groupSize - 1))
let myGroup = myRawData.Skip(i).Take(groupSize)
select new
{
Values = string.Join(",", myGroup.Select(rd => rd.PriceRaw.ToString())),
Average = myGroup.Average(rd => rd.PriceRaw)
};
}
public class Data_Raw
{
public DateTime DateDataRaw { set; get; }
public double PriceRaw { set; get; }
public double PercentageReturnsRaw { set; get; }
public Data_Raw(DateTime _dateDataRaw, double _priceRaw, double _percentageReturnsRaw)
{
DateDataRaw = _dateDataRaw; //1
PriceRaw = _priceRaw;
PercentageReturnsRaw = _percentageReturnsRaw;
}
}
answered Nov 8 at 14:09
Cetin Basoz
10.8k11526
10.8k11526
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53189310%2flinq-skip-and-take-on-class-list%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
So what is the issue? Why don't you do it in the way you talked about at the top?
– rory.ap
Nov 7 at 12:18
It would be awesome if you could provide a Minimal, Complete, and Verifiable example including sample data, and specify the exact results you are wanting to achieve.
– mjwills
Nov 7 at 12:57