我在WebApi中使用Linq to SQL将数据库中的对象列表返回到前端。
假设模型看起来像这样:
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; }
}
另外,我在一个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()
...
...
现在我需要以递增的顺序通过Schedules.start的minValue命令“list”。这意味着,输出应该首先给出最早开始的元素。此外,最后应该给出没有计划时间表的课程。
最后输出应如下所示:
[{“ID”:5,“NAME”:“NAME1”,“Scheduleles”:[{“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”,“Scheduleles”:[{“id”:9,“courseid”:1,“start”:“ 2017-12-16 00:00:00.000 “},{”id“:2,”courseid“:1,”start“:” 2017-12-17 00:00:00.000 “}} }]
提前致谢。
答案 0 :(得分:2)
我遇到这种情况,我需要根据子属性的值对父进行排序。
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()