ASP.Net IEnumerable List: store values to chart
I have an array of my x values for my chart and a Linq to Entity LIST that contains my y values. How do I access my list to add their values to the chart. this is what I have so far:
//Get restaurant name & count of votes for each restaurant
BL.BLaddVote obj1 = new BLaddVote();
var votesList = obj1.countVotes();
//Set chart x & y values: here is where I'm stuck
chtVotes.Series[0].Points.Add(<X VALUES>, <Y VALUES>);
How do I get the values from my anonlymous list into my chart? Thanks in advance.
Additionally, here is the query to pull the data:
public class NumVotesInfo
{
public string RestName { get; set; }
public int NumVotes { get; set; }
}
public IEnumerable<NumVotesInfo> countVotes()
{
//Return the count of the number of reviews for a specific restaurant ID
var NumVotes = from VOTE in db.VOTEs
group VOTE by VOTE.VOTE_VALUE into t
selec开发者_JAVA百科t new NumVotesInfo { RestName = t.Key, NumVotes = t.Count() };
return NumVotes.ToList();
}
Seems like you want to merge the list of X values and the list of Y values:
var pointList = myXValues.Zip(votesList, (a,b) => new { X = a, Y = b.NumVotes });
Now you have X
and Y
properties in your pointList
and can use it for charting:
foreach(var point in pointList)
chtVotes.Series[0].Points.Add(point.X, point.Y);
Alternatively, assuming still that both lists have the same length you can just use the index. This would require countVotes()
to return a list not an IEnumerable
, you can create the list by using ToList()
:
var votesList = obj1.countVotes().ToList();
Now you can just use the index:
for(int i = 0; i< votesList.Count, i++)
{
chtVotes.Series[0].Points.Add(myXValues[i], votesList[i].NumVotes);
}
精彩评论