我有以下嵌套类。我试图在DisplayOrder字段
的适当位置进行排序public class Menu
{
public virtual string MenuDescription { get; set; }
public virtual bool Active { get; set; }
public virtual short SiteId { get; set; }
public virtual IEnumerable<MenuHeader> MenuHeaders { get; set; }
}
public class MenuHeader
{
public virtual string HeaderName { get; set; }
public virtual string Url { get; set; }
public virtual string DisplayOrder { get; set; }
public virtual IEnumerable<MenuCategory> MenuCategories { get; set; }
}
public class MenuCategory
{
public virtual string Name { get; set; }
public virtual string Icon { get; set; }
public virtual string Url { get; set; }
public virtual string DisplayOrder { get; set; }
public virtual IEnumerable<MenuOptionHeader> MenuOptionHeaders { get; set; }
}
public class MenuOptionHeader
{
public virtual string Name { get; set; }
public virtual string Url { get; set; }
public virtual string DisplayOrder { get; set; }
public virtual IEnumerable<MenuOption> MenuOptions { get; set; }
}
public class MenuOption
{
public virtual string Name { get; set; }
public virtual string Url { get; set; }
public virtual string DisplayOrder { get; set; }
}
我很难通过DisplayOrder
返回每个级别排序的所有数据我正在尝试像
这样的东西menuRepository.GetAll(x => x.SiteId == _siteConfiguration.Id && x.Active == true)
.SelectMany(x => x.MenuHeaders.OrderBy(d => d.DisplayOrder))
.SelectMany(x1 => x1.MenuCategories.OrderBy(o => o.DisplayOrder))
.SelectMany(x2 => x2.MenuOptionHeaders.OrderBy(o => o.DisplayOrder));
这仅返回零件数据集。如何修改linq以返回DisplayOrder在每个级别排序的所有数据
答案 0 :(得分:1)
您在收集项目上调用OrderBy
这一事实并未在不重新分配结果的情况下对其进行排序。在您的示例中,您需要额外的变量来存储订购的商品。您的商品是否与商店断开连接?如果是这样,你可以这样做:
List<Menu> orderedItems = menuRepository
.GetAll(x => x.SiteId == _siteConfiguration.Id && x.Active == true)
.ToList()
.ForEach(m =>
{
m.MenuHeaders = m.MenuHeaders.OrderBy(d => d.DisplayOrder);
m.MenuCategories = m.MenuCategories.OrderBy(d => d.DisplayOrder);
m.MenuOptionHeaders = m.MenuOptionHeaders.OrderBy(d => d.DisplayOrder);
});
确实会将IOrderedEnumerable
分配给嵌套属性。