简体   繁体   English

C#-Linq to SQL-通过嵌套类的元素对列表进行排序

[英]C# - Linq to SQL - Order a List by an element of a nested class

I am using Linq to SQL in a WebApi to return a list of objects from a database to a frontend. 我在WebApi中使用Linq to SQL将数据库中的对象列表返回到前端。

Let's say the model looks something like this: 假设模型看起来像这样:

public class Course
{
    public int ID { get; set; }
    public string NAME { get; set; }       
}
public class Schedules
{
    public int id { get; set; }
    public int courseid
    public datetime start { get; set; }
} 

In addition my Linq-to-SQL inside of one Controller looks something like this: 另外,我在一个Controller内部的Linq-to-SQL看起来像这样:

...
...
var list =    (from xx in xx.Courses
              select new Course
              {
                  ID = xx.ID,
                  NAME = xx.NAME,
                  Schedules = (from yy in yy.Schedules
                              where xx.ID == yy.courseid
                              select new Schedules
                              {
                                 id = yy.id,
                                 courseid = yy.courseid,
                                 start = yy.start
                              }).toList()
              }).toList()
              ...
              ...

Now I need to order "list" by the minValue of Schedules.start in an ascending order. 现在,我需要按Schedules.start的minValue升序对“列表”进行排序。 That means, that the output should give me the element with the earliest start first. 就是说,输出应该给我最早开始的元素。 In addition Courses with no planned schedule should be given out at the end. 此外,没有计划时间表的课程应在最后给出。

In the end the output should look like this: 最后,输出应如下所示:

[{"ID":5, "NAME":"NAME1", "Schedules":[{"id":10, "courseid":5, "start":" 2017-12-15 00:00:00.000 "}, {"id":8, "courseid":5, "start":" 2017-12-20 00:00:00.000 "}]}],[{"ID":1, "NAME":"NAME1", "Schedules":[{"id":9, "courseid":1, "start":" 2017-12-16 00:00:00.000 "}, {"id":2, "courseid":1, "start":" 2017-12-17 00:00:00.000 "}]}] [{“ ID”:5,“ NAME”:“ NAME1”,“ Schedules”:[{“ id”:10,“ courseid”:5,“开始”:“ 2017-12-15 00:00:00.000 ” },{“ id”:8,“ courseid”:5,“ start”:“ 2017-12-20 00:00:00.000 ”}]}] ,, [{“ ID”:1,“ NAME”:“ NAME1 “,” Schedules“:[{” id“:9,” courseid“:1,” start“:” 2017-12-16 00:00:00.000 “},{” id“:2,” courseid“:1 ,“开始”:“ 2017-12-17 00:00:00.000 “}]}]

Thanks in advance. 提前致谢。

I was in such a situation where I need to sort the parent depend on the child property's value. 在这种情况下,我需要根据子级属性的值对父级进行排序。

var list =    (from xx in xx.Courses
              select new Course
              {
                  ID = xx.ID,
                  NAME = xx.NAME,
                  Schedules = (from yy in yy.Schedules
                              where xx.ID == yy.courseid
                              select new Schedules
                              {
                                 id = yy.id,
                                 courseid = yy.courseid,
                                 start = yy.start
                              }).toList()
              }).OrderBy(mc => mc.Schedules.Min(dc => dc.start)).toList()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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