0

我无法针对以下问题获取正确的 Linq 查询:我有一个包含以下数据的故事点列表<>:

ParentID    TaskID      Points
--------    ------    -----------
    1         100          2
    1         101          1
    1         103          1
    2         102          1
    2         104          4
    3         105          1

我正在寻找具有 StoryPoints 总计数的不同 ParentID 列表:

ParentID    Count
--------    -----
   1          4
   2          5
   3          1

我试过这个块,但语法抛出错误:

var storytaskpoints =
                        from u in storypoints
                        group u by u.ParentID into g
                        select new
                        {
                            ParentID = g.Key,
                            Count = (from u in g select u.Points).Count();
                        }

我的 StoryTaskPoint 课程:

class StoryTaskPoint
    {
        public int ParentID { get; set; }
        public int Points { get; set; }
        public int TaskID { get; set; }

        public StoryTaskPoint()
        { }

        StoryTaskPoint(
            int taskID,
            int parentID,
            int points)
        {
            TaskID = taskID;
            ParentID = parentID;
            Points = points;
        }
    }

任何帮助,将不胜感激....

4

1 回答 1

1
 from u in storypoints
                        group u by u.ParentID into g
                        select new
                        {
                            ParentID = g.Key,
                            Count = g.Sum(gg=>gg.Points)
                        }

使用.Sum代替.Count

于 2019-05-30T19:20:37.400 回答