选择分组数据linq中的所有列

时间:2018-05-25 17:16:25

标签: c# linq grouping

嗨我有linq查询我想要排序和总结所有基于studentId的项目所以经过一些搜索后我发现了这个:

 var res = _initialCollection.GroupBy(x => new { x.StudentId })
                        .Select(x => new
                        {
                            x.Key.StudentId,
                            Sum = x.Sum(y => AppVariable.EnumToNumber(y.Scores))
                        }).ToArray();

现在我想将此查询绑定到datagrid,但我无法获取其他列数据。 这是我的表:

public long Id { get; set; }
            public long BaseId { get; set; }
            public long StudentId { get; set; }
            public string Name { get; set; }
            public string LName { get; set; }
            public string FName { get; set; }
            public string Scores { get; set; }

我如何在此查询中选择其他列? UPDATE:

 var res = _initialCollection.GroupBy(x => new { x.StudentId })
                            .Select(x => new
                            {
                                x.Key.StudentId,
                                Sum = x.Sum(y => AppVariable.EnumToNumber(y.Scores))
                            }).ToArray();

2 个答案:

答案 0 :(得分:0)

我们可以使用以下程序来有效地使用LINQ,请注意我们已使用OrderBy按升序排序(使用OrderByDescending进行降序排序)。希望它有所帮助。

using System;
using System.Collections.Generic;
using System.Linq;

namespace Solutions
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Student> students = new List<Student>()
                                         {
                                             new Student(){BaseId = 100, FName = "A", Id = 1, LName = "Z", Name = "A Z", Scores = "90", StudentId = 1},
                                             new Student(){BaseId = 100, FName = "A", Id = 2, LName = "Z", Name = "A Z", Scores = "10", StudentId = 1},
                                             new Student(){BaseId = 100, FName = "A", Id = 3, LName = "Z", Name = "A Z", Scores = "10", StudentId = 1},
                                             new Student(){BaseId = 100, FName = "B", Id = 2, LName = "Y", Name = "B Y", Scores = "91", StudentId = 2},
                                             new Student(){BaseId = 100, FName = "C", Id = 3, LName = "X", Name = "C X", Scores = "92", StudentId = 3},
                                             new Student(){BaseId = 100, FName = "D", Id = 4, LName = "W", Name = "D W", Scores = "93", StudentId = 4},
                                             new Student(){BaseId = 100, FName = "E", Id = 5, LName = "V", Name = "E V", Scores = "91", StudentId = 5},
                                         };

            // Total Score Select and Order By in the end for sorting
            var totalScoresSelect = students.GroupBy(student => student.StudentId).Select(
                groupedStudent => new
                {
                    StudentId = groupedStudent.Key,
                    TotalScore = groupedStudent.Sum(student => long.Parse(student.Scores)),
                    groupedStudent.First().FName,
                    groupedStudent.First().LName,
                    groupedStudent.First().Name
                }).OrderBy(totalScore => totalScore.TotalScore);

            foreach (var totalScore in totalScoresSelect)
            {
                Console.WriteLine(totalScore.StudentId);
                Console.WriteLine(totalScore.FName);
                Console.WriteLine(totalScore.LName);
                Console.WriteLine(totalScore.Name);
                Console.WriteLine(totalScore.TotalScore);
            }

            Console.ReadLine();
        }
    }
}

答案 1 :(得分:0)

var res = _initialCollection.GroupBy(x => new { x.StudentId })
                            .Select(x => new 
                            {
                              StudentId=  x.Key,
                              Sum = x.Sum(y => AppVariable.EnumToNumber(y.Scores))
                              x.First().FName
                            }).ToList();