我正在尝试按项目ID分组,以便我可以返回总和,但是当我尝试使用First()返回字段的值时,它只返回一个值。ASP.net c#项目尝试返回字段的总和时,我使用LINQ的代码返回正确的数字,但我似乎无法返回相应的字符串字段。我尝试按2个字段分组,但这会返回太多记录。public class JobsByDrivetime{private string pid;private double jobMinutes;private string jobEventType;public JobsByDrivetime(string pid, double jobMinutes,string jobEventType){ this.pid = pid; this.jobMinutes = jobMinutes; this.jobEventType = jobEventType;}public string Pid{ get { return pid; } set { pid = value; }}public double JobMinutes{ get { return jobMinutes; } set { jobMinutes = value; }}public string JobEventType{ get { return jobEventType; } set { jobEventType = value; }}}List<JobsByDrivetime> ProjectbyDrivetime = new List<JobsByDrivetime>();ProjectbyDrivetime.Add(new JobsByDrivetime("0186975-0001", 439, "HR"));ProjectbyDrivetime.Add(new JobsByDrivetime("0186975-0001", 100, "HR"));ProjectbyDrivetime.Add(new JobsByDrivetime("0186975-0001", 180, "JR"));var projectBydrivetimeList = ProjectbyDrivetime.GroupBy(v => v.Pid) //(c => new { Pid = c.Pid, ProjectType = c.JobEventType }).Select(g => new{ Pid = g.Key, ProjectMinutes = g.Sum(x => x.JobMinutes), ProjectType=g.Select(x=> x.JobEventType).First()}).ToList();foreach (var D in projectBydrivetimeList){ shiftSummary.Add("<tr><td>" + D.Pid + "</td><td>" + D.ProjectType + "</td><td>" + D.ProjectMinutes + "</td></tr>");}输出0186975-0001 HR 5390186975-0001 HR 180它(按项目类型)正确分组项目分钟,但它仅显示2个项目类型中的一种。我希望D.ProjectType在第一个是HR,在第二个是JR。我明白这是因为我正在使用First()。
添加回答
举报
0/150
提交
取消