简体   繁体   中英

Linq that Aggregates distinct IDs

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....

 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM