我无法针对以下问题获取正确的 Linq 查询:我有一个包含以下数据的故事点列表<>:
ParentID TaskID Points
-------- ------ -----------
1 100 2
1 101 1
1 103 1
2 102 1
2 104 4
3 105 1
我希望获得具有故事点聚合计数的不同父 ID 列表:
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();
}
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;
}
}
任何帮助将不胜感激...
from u in storypoints
group u by u.ParentID into g
select new
{
ParentID = g.Key,
Count = g.Sum(gg=>gg.Points)
}
使用.Sum
而不是.Count