ASP.Net IEnumerable List:存储图表的值



我有一个图表的x值数组和一个包含y值的Linq to Entity LIST。我如何访问我的列表以将它们的值添加到图表中。这是我目前所看到的:

                //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>);

我如何从我的匿名列表的值到我的图表?提前谢谢。

另外,下面是提取数据的查询:

    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
                         select new NumVotesInfo { RestName = t.Key, NumVotes = t.Count() };
        return NumVotes.ToList();
    }

似乎你想要合并X值列表和Y值列表:

var pointList = myXValues.Zip(votesList, (a,b) => new { X = a, Y = b.NumVotes });

现在你有XY属性在你的pointList和可以使用它的图表:

foreach(var point in pointList)
    chtVotes.Series[0].Points.Add(point.X, point.Y);

或者,假设两个列表的长度相同,您可以直接使用索引。这将要求countVotes()返回一个列表,而不是IEnumerable,您可以使用ToList()创建列表:

var votesList = obj1.countVotes().ToList();

现在你可以直接使用索引:

for(int i = 0; i< votesList.Count, i++)
{
      chtVotes.Series[0].Points.Add(myXValues[i], votesList[i].NumVotes);
}

最新更新