Linq that Aggregates distinct IDs

64 views Asked by At

I am having trouble getting the correct Linq query for the following issue: I have a List<> of storypoints that contains the following data:

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

I'm looking to get a list of distinct ParentIDs with an Aggregate count of StoryPoints:

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

I've tried this block but the syntax is throwing an error:

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();
                        }

My Class for 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;
        }
    }

Any help would be appreciated....

1

There are 1 answers

0
AD.Net On BEST ANSWER
 from u in storypoints
                        group u by u.ParentID into g
                        select new
                        {
                            ParentID = g.Key,
                            Count = g.Sum(gg=>gg.Points)
                        }

Use .Sum instead of .Count