使用 Fluent API 按 属性 .NET Core 3.1 对列表进行分组
Grouping list by property .NET Core 3.1 using Fluent API
我需要根据列表 属性 对数据进行分组。下面的代码是我当前场景的模拟:
using System;
using System.Collections.Generic;
using System.Linq;
using static teste.StudentClass;
namespace teste
{
class Program
{
public static void Main(string[] args)
{
List<Student> students = new List<Student>
{
new Student {FirstName = "Terry", LastName = "Adams", ID = 120,
Year = GradeLevel.SecondYear,
ExamScores = new List<int>{ 99, 82, 81, 79}},
new Student {FirstName = "Fadi", LastName = "Fakhouri", ID = 116,
Year = GradeLevel.ThirdYear,
ExamScores = new List<int>{ 99, 86, 90, 94}},
new Student {FirstName = "Hanying", LastName = "Feng", ID = 117,
Year = GradeLevel.FirstYear,
ExamScores = new List<int>{ 93, 92, 80, 87}},
new Student {FirstName = "Cesar", LastName = "Garcia", ID = 114,
Year = GradeLevel.FourthYear,
ExamScores = new List<int>{ 97, 89, 85, 82}},
new Student {FirstName = "Debra", LastName = "Garcia", ID = 115,
Year = GradeLevel.ThirdYear,
ExamScores = new List<int>{ 35, 72, 91, 70}},
new Student {FirstName = "Hugo", LastName = "Garcia", ID = 118,
Year = GradeLevel.SecondYear,
ExamScores = new List<int>{ 92, 90, 83, 78}},
new Student {FirstName = "Sven", LastName = "Mortensen", ID = 113,
Year = GradeLevel.FirstYear,
ExamScores = new List<int>{ 88, 94, 65, 91}},
new Student {FirstName = "Claire", LastName = "O'Donnell", ID = 112,
Year = GradeLevel.FourthYear,
ExamScores = new List<int>{ 75, 84, 91, 39}},
new Student {FirstName = "Svetlana", LastName = "Omelchenko", ID = 111,
Year = GradeLevel.SecondYear,
ExamScores = new List<int>{ 97, 92, 81, 60}},
new Student {FirstName = "Lance", LastName = "Tucker", ID = 119,
Year = GradeLevel.ThirdYear,
ExamScores = new List<int>{ 68, 79, 88, 92}},
new Student {FirstName = "Michael", LastName = "Tucker", ID = 122,
Year = GradeLevel.FirstYear,
ExamScores = new List<int>{ 94, 92, 91, 91}},
new Student {FirstName = "Eugene", LastName = "Zabokritski", ID = 121,
Year = GradeLevel.FourthYear,
ExamScores = new List<int>{ 96, 85, 91, 60}}
};
students.ToList().ForEach(x =>
{
Console.WriteLine($"\t{x.FirstName}, {x.LastName}, {x.Year}");
});
}
}
public class StudentClass
{
public enum GradeLevel { FirstYear = 1, SecondYear, ThirdYear, FourthYear };
public class Student
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int ID { get; set; }
public GradeLevel Year;
public List<int> ExamScores;
}
}
}
目前,我的结果如下:
Terry, Adams, SecondYear
Fadi, Fakhouri, ThirdYear
Hanying, Feng, FirstYear
Cesar, Garcia, FourthYear
Debra, Garcia, ThirdYear
Hugo, Garcia, SecondYear
Sven, Mortensen, FirstYear
Claire, O'Donnell, FourthYear
Svetlana, Omelchenko, SecondYear
Lance, Tucker, ThirdYear
Michael, Tucker, FirstYear
Eugene, Zabokritski, FourthYear
我必须更改 return 以便数据按 LatsName
属性 分组。预期的输出。我如何使用 Fluent Api 来做到这一点?预期输出(示例):
{LastName:"Garcia": {FirstName:"Cesar", Year: "FourthYear"}, {FirstName:"Debra", Year: "ThirdYear"}, {FirstName:"Hugo", Year: "SecondYear"}},
{LastName:"Tucker": {FirstName:"Lance", Year: "ThirdYear"}, {FirstName:"Michael", Year: "FirstYear"}}
....
我正在尝试一个简单的 groupBy,但在转换时 return 出现错误消息:
students = students.GroupBy(x => x.LastName);
错误信息:
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<System.Linq.IGrouping<string, teste.StudentClass.Student>>' to 'System.Collections.Generic.List<teste.StudentClass.Student>'. An explicit conversion exists (are you missing a cast?)
您的变量 students
可能是 List<Student>
类型,因此您尝试进行的转换是不可能的,因为 GroupBy
returns IEnumerable<IGrouping<TKey, TSource>>
.
这可能是您正在寻找的解决方案
public class StudentDto
{
public string FirstName { get; set; }
public GradeLevel Year { get; set; }
}
public class StudentGroup
{
public string LastName { get; set; }
public IEnumerable<StudentDto> Students { get; set; }
}
现在可以这样分组了
IEnumerable<StudentGroup> groupedData = students.GroupBy(x => x.LastName)
.Select(x => new StudentGroup {
LastName = x.Key,
Students = x.Select(student => new StudentDto
{
FirstName = student.FirstName,
Year = student.Year
}).ToList()
});
通过这种方式,您可以将所有学生按姓氏分组。
我需要根据列表 属性 对数据进行分组。下面的代码是我当前场景的模拟:
using System;
using System.Collections.Generic;
using System.Linq;
using static teste.StudentClass;
namespace teste
{
class Program
{
public static void Main(string[] args)
{
List<Student> students = new List<Student>
{
new Student {FirstName = "Terry", LastName = "Adams", ID = 120,
Year = GradeLevel.SecondYear,
ExamScores = new List<int>{ 99, 82, 81, 79}},
new Student {FirstName = "Fadi", LastName = "Fakhouri", ID = 116,
Year = GradeLevel.ThirdYear,
ExamScores = new List<int>{ 99, 86, 90, 94}},
new Student {FirstName = "Hanying", LastName = "Feng", ID = 117,
Year = GradeLevel.FirstYear,
ExamScores = new List<int>{ 93, 92, 80, 87}},
new Student {FirstName = "Cesar", LastName = "Garcia", ID = 114,
Year = GradeLevel.FourthYear,
ExamScores = new List<int>{ 97, 89, 85, 82}},
new Student {FirstName = "Debra", LastName = "Garcia", ID = 115,
Year = GradeLevel.ThirdYear,
ExamScores = new List<int>{ 35, 72, 91, 70}},
new Student {FirstName = "Hugo", LastName = "Garcia", ID = 118,
Year = GradeLevel.SecondYear,
ExamScores = new List<int>{ 92, 90, 83, 78}},
new Student {FirstName = "Sven", LastName = "Mortensen", ID = 113,
Year = GradeLevel.FirstYear,
ExamScores = new List<int>{ 88, 94, 65, 91}},
new Student {FirstName = "Claire", LastName = "O'Donnell", ID = 112,
Year = GradeLevel.FourthYear,
ExamScores = new List<int>{ 75, 84, 91, 39}},
new Student {FirstName = "Svetlana", LastName = "Omelchenko", ID = 111,
Year = GradeLevel.SecondYear,
ExamScores = new List<int>{ 97, 92, 81, 60}},
new Student {FirstName = "Lance", LastName = "Tucker", ID = 119,
Year = GradeLevel.ThirdYear,
ExamScores = new List<int>{ 68, 79, 88, 92}},
new Student {FirstName = "Michael", LastName = "Tucker", ID = 122,
Year = GradeLevel.FirstYear,
ExamScores = new List<int>{ 94, 92, 91, 91}},
new Student {FirstName = "Eugene", LastName = "Zabokritski", ID = 121,
Year = GradeLevel.FourthYear,
ExamScores = new List<int>{ 96, 85, 91, 60}}
};
students.ToList().ForEach(x =>
{
Console.WriteLine($"\t{x.FirstName}, {x.LastName}, {x.Year}");
});
}
}
public class StudentClass
{
public enum GradeLevel { FirstYear = 1, SecondYear, ThirdYear, FourthYear };
public class Student
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int ID { get; set; }
public GradeLevel Year;
public List<int> ExamScores;
}
}
}
目前,我的结果如下:
Terry, Adams, SecondYear
Fadi, Fakhouri, ThirdYear
Hanying, Feng, FirstYear
Cesar, Garcia, FourthYear
Debra, Garcia, ThirdYear
Hugo, Garcia, SecondYear
Sven, Mortensen, FirstYear
Claire, O'Donnell, FourthYear
Svetlana, Omelchenko, SecondYear
Lance, Tucker, ThirdYear
Michael, Tucker, FirstYear
Eugene, Zabokritski, FourthYear
我必须更改 return 以便数据按 LatsName
属性 分组。预期的输出。我如何使用 Fluent Api 来做到这一点?预期输出(示例):
{LastName:"Garcia": {FirstName:"Cesar", Year: "FourthYear"}, {FirstName:"Debra", Year: "ThirdYear"}, {FirstName:"Hugo", Year: "SecondYear"}},
{LastName:"Tucker": {FirstName:"Lance", Year: "ThirdYear"}, {FirstName:"Michael", Year: "FirstYear"}}
....
我正在尝试一个简单的 groupBy,但在转换时 return 出现错误消息:
students = students.GroupBy(x => x.LastName);
错误信息:
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<System.Linq.IGrouping<string, teste.StudentClass.Student>>' to 'System.Collections.Generic.List<teste.StudentClass.Student>'. An explicit conversion exists (are you missing a cast?)
您的变量 students
可能是 List<Student>
类型,因此您尝试进行的转换是不可能的,因为 GroupBy
returns IEnumerable<IGrouping<TKey, TSource>>
.
这可能是您正在寻找的解决方案
public class StudentDto
{
public string FirstName { get; set; }
public GradeLevel Year { get; set; }
}
public class StudentGroup
{
public string LastName { get; set; }
public IEnumerable<StudentDto> Students { get; set; }
}
现在可以这样分组了
IEnumerable<StudentGroup> groupedData = students.GroupBy(x => x.LastName)
.Select(x => new StudentGroup {
LastName = x.Key,
Students = x.Select(student => new StudentDto
{
FirstName = student.FirstName,
Year = student.Year
}).ToList()
});
通过这种方式,您可以将所有学生按姓氏分组。