1

我有一个从 Access DB 填充的数据表。结果看起来像

Month | Sum
--------------
1 | 1464
2 | 1716
3 | 2125
4 | 2271
5 | 2451
6 | 2583
7 | 2671
9 | 2823
10 | 2975

你是对的 - 八月什么都没有!我想要的是,八月使用与七月相同的值。目前我正在使用这个 LINQ 查询将数据添加到折线图中:

for (int i = 1; i <= System.DateTime.Now.Month; i++)
            {
                var numbers = (from p in dTable.AsEnumerable()
                              where p.Field<int>("M") >= i
                              select p).First();                   
                series2.Points.Add(new DataPoint { AxisLabel = i.ToString(), YValues = new double[] { Convert.ToDouble(numbers["Sum"]) } });  
            }

图表已显示,但 8 月使用的是 9 月的值。我认为这是我做错的非常基本的事情,但我根本无法弄清楚。提前致谢!

4

2 回答 2

4

您正在请求大于当前月份的所有月份。

where p.Field<int>("M") >= i

因此,对于 8 月 (8),您正在检索 9 月及更大的时间 (9、10、11、12),而不是 7 月 (7)。

您必须颠倒您的约束,并按月份降序排列:

var numbers = (from p in dTable.AsEnumerable()
                          where p.Field<int>("M") <= i
                          select p)
                         .OrderByDesc(p => p.Month) 
                         .First();   
于 2013-10-28T16:15:44.243 回答
2

你必须颠倒你的逻辑:

var numbers = (from p in dTable.AsEnumerable()
               where p.Field<int>("M") <= i
               select p).Last();

不用说,当没有上个月时,这不起作用。

更新:

以上假设您正在阅读的表格是有序的。如果不是这种情况,您必须自己订购(如 Cyril Gandon 所述):

var numbers = (from p in dTable.AsEnumerable()
               where p.Field<int>("M") <= i
               orderby p.Field<int>("M") descending
               select p).First();
于 2013-10-28T16:25:35.450 回答