【发布时间】:2019-05-02 21:29:31
【问题描述】:
我有两个班级要一起参加:
物品
(一堆属性)
(外键)int类别
类别
(主键)int ID(在Item中引用)
字符串名称
在我的 MVC 5 项目中使用 LINQ,我需要使用 Item 表中的 ID 从类别表中获取 Name 值并按其排序。使用下面的代码,它按整数 (1,2,3,4) 的顺序排序,而不是按 Category 表中的 Name 属性。如何按类别表排序?
控制器代码:
var items = from s in db.Items
join c in db.Categories on s.Category equals c.ID
select s;
switch (sortOrder)
{
case "Category_Desc":
//This is where it is selecting the integer ID instead of the string Name
items = items.OrderByDescending(s => s.Category);
break;
default:
items = items.OrderBy(s => s.Category);
break;
}
【问题讨论】:
标签: asp.net-mvc linq