sql - Entity framework中如何OrderBy引用表列

标签 sql database linq entity-framework

我有 2 个引用表,例如:Order & Product
我正在使用英孚。现在我有一个页面,它显示如下:

订单号 |订购日期 |产品名称 |价格


产品表 (ProductID) 有一个外键,我正在显示 o.Product.ProductName
现在我想按产品表中的产品名称OrderBy 列表。
我该怎么做?
仅供引用:Order 表中的 ProductIDNullable

提前致谢

最佳答案

看来你在 Order 类中有一个导航属性 Product

所以你可以做

.OrderBy(o => (o.Product == null) ? string.Empty : o.Product.ProductName)

你也可以分两次完成

.OrderByDescending(o => o.Product == null) //or OrderBy, depending if you want null first or last
.ThenBy(o => (o.Product == null) ? string.Empty : o.Product.ProductName); 

关于sql - Entity framework中如何OrderBy引用表列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21086935/

相关文章:

mysql - 子查询 'in' 操作数应包含 1 列

java - 赋予 firebase 数据库名称等于 USER ID 键的子级

mysql - 导出和导入用户和数据库整理问题

c# - 将 charArray 转换为 byteArray

c# - Linq 左连接的问题

sql - 如何使用左外连接和 tableB 的 where 条件从 tableA 中获取所有记录?

php - 在一列上选择 DISTINCT,在 mysql 中有多个列

mysql - 查找重复帐户

mysql - 更新mysql中的余额

c# - 使用 LINQ 进行全文搜索