1

我无法获得此 linq 的正确结果。

 var transactions = from trans in tblMemberInfo.AsEnumerable()
                    orderby trans.Field<Int32>("AGE") ascending
                    group trans by trans.Field<Int32>("AGE") into groupTrans

                    select new
                    {                                      
                         ZeroToSix = groupTrans.Count(age => age.Field<Int32>("AGE") >= 0 && age.Field<Int32>("AGE") <= 6) 
                    };

我有一个数据表,其中包含以下数据。现在,我想总结年龄在 0 到 5 之间的所有年龄计数。

AGE     Count
0       6
1       6
2       7
3       5
4       5
5       20
6       5
7       5
8       5
9       5
10      5

我想看到这样的结果。

Age

ZeroToFive = 49
AboveSix = 25
4

1 回答 1

2

检查这个:

// mock data
var data = new List<dynamic> {
    new { Age = 0, Count = 6 },
    new { Age = 1, Count = 6 },
    new { Age = 2, Count = 7 },
    new { Age = 3, Count = 5 },
    new { Age = 4, Count = 5 },
    new { Age = 5, Count = 20 },
    new { Age = 6, Count = 5 },
    new { Age = 7, Count = 5 },
    new { Age = 8, Count = 5 },
    new { Age = 9, Count = 5 },
    new { Age = 10, Count = 5 },
};

var age = new { 
                ZeroToFive = data.Where(x => x.Age < 6).Sum(x => x.Count),
                AboveSix = data.Where(x => x.Age >= 6).Sum(x => x.Count)
              };
于 2013-07-04T07:09:38.363 回答