gpt4 book ai didi

asp.net - Linq 按子表排序

转载 作者:行者123 更新时间:2023-12-02 21:33:41 24 4
gpt4 key购买 nike

我的问题是如何按子表对 Linq 查询进行排序:

表格应用程序:

- app_id
- name

表应用程序状态:

 - app_status_id
- app_id
- severity
- status_date

我想查询所有应用程序,按最后状态严重性排序:

app_id  name
1 first
2 second
3 third

app_status_id app_id severity status_date
1 1 5 12-4-2010
2 1 2 15-4-2010
3 2 7 10-4-2010
4 3 3 13-4-2010

Now i want it sorted like:
app_id name
3 third
1 first
2 second

任何人都可以帮我解决这个问题的 LINQ 查询吗?

我已经尝试了以下方法,但没有成功:

    var apps = from apps in dc.Apps
orderby apps.AppStatus.LastOrDefault().severity
select apps;

编辑:

我将完善我的问题,它应该首先获取具有最新状态的所有应用程序(因此按状态日期),然后应该按最后状态的严重性对该列表进行排序。

最佳答案

试试这个:

var apps = from apps in dc.Apps
orderby apps.AppStatus.Max(a => a.severity)
select apps;

根据您的评论进行编辑:

var apps = from apps in dc.Apps
where apps.AppStatus.Count() > 0 // to prevent null reference below
orderby apps.AppStatus.Where(a1 => a1.status_date ==
apps.AppStatus.Max(a2 => a2.status_date)
).Single().severity
select apps;

关于asp.net - Linq 按子表排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2651846/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com