Asked by:
LINQ orderby in include

Question
-
hello everyone i want to make an order by ascending inside the include can anyone help me?
// Models
public class Menu
{
public int MenuID { get; set; }
public String MenuHead { get; set; }
public int Sorting { get; set; }
public ICollection<MenuDetails> MenuDetails { get; set; }
Menu() {
MenuDetails = new List<MenuDetails>();
}
}
public class MenuDetails
{
public int MenuDetailsId { get; set; }
public String Title { get; set; }
public int Sorting { get; set; }
public Menu Menu { get; set; }
public int MenuID { get; set; }
}
//resources
public class MenuResource
{
public int MenuID { get; set; }
public String MenuHead { get; set; }
public int Sorting { get; set; }
public ICollection<MenuDetails> MenuDetails { get; set; }
Menu() {
MenuDetails = new List<MenuDetails>();
}
}
public class MenuDetailsResource
{
public int MenuDetailsId { get; set; }
public String Title { get; set; }
public int Sorting { get; set; }
}// controller
[HttpGet]
public async Task<IEnumerable<MenuResource>> Get()
[ActionName("All")]
{var menus = await Context.Menu.Include(m => m.MenuDetails).Where(s => s.Status == 0).OrderBy(o => o.Sorting).ToListAsync();
return Mapper.Map<List<Menu>, List<MenuResource>>(menus);
}Tuesday, July 23, 2019 6:22 AM
All replies
-
MODEL
public class Menu {
public int MenuID { get; set; }
public String MenuHead { get; set; }
public int Sorting { get; set; }
public ICollection<MenuDetails> MenuDetails { get; set; }
Menu() {
MenuDetails = new List<MenuDetails>();
}}
public class MenuDetails {
public int MenuDetailsId { get; set; }public String PageTitle { get; set; }
public Menu Menu { get; set; }
public int Sorting { get; set; }
}
RESOURCES
public class MenuDetailsResource
{
public int MenuDetailsId { get; set; }
public String PageTitle { get; set; }
public int Sorting { get; set; }
}
CONTROLLER
[HttpGet]
[ActionName("All")]public async Task<IEnumerable<MenuResource>> Get()
{var menus = await Context.Menu.Include(m => m.MenuDetails).Where(s => s.Status == 0).OrderBy(o => o.Sorting).ToListAsync();
return Mapper.Map<List<Menu>, List<MenuResource>>(menus);
}
how can i order in MENUDETAILS?
- Merged by Zhanglong WuMicrosoft contingent staff Thursday, August 8, 2019 6:56 AM duplicate
Tuesday, July 9, 2019 6:32 AM -
Hi,
In the Linq statement, I noticed there is a property named "Status". But I could not find it in the class "MenuDetails". Could you explain it in detail?
Regards,
Kyle
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.Wednesday, July 10, 2019 7:11 AM -
there is "Status" property in the "menu" class i just forget to copy
Tuesday, July 23, 2019 6:25 AM -
this code
var menus = await Context.Menu.Include(m => m.MenuDetails).Where(s => s.Status == 0).OrderBy(o => o.Sorting).ToListAsync();
order the Menus by Menu.Sorting, and this code
var menus = await Context.Menu.Include(m => m.MenuDetails).Where(s => s.Status == 0).OrderBy(o => o.MenuDetails.OrderBy(b=>b.Sorting).FirstOrDefault().Sorting ).ToListAsync();
order the Menus by MenuDetails.sorting
i want to order by Menu.Sorting and MenuDetails.Sorting
Tuesday, July 23, 2019 8:30 AM -
this code
var menus = await Context.Menu.Include(m => m.MenuDetails).Where(s => s.Status == 0).OrderBy(o => o.Sorting).ToListAsync();
order the Menus by Menu.Sorting, and this code
var menus = await Context.Menu.Include(m => m.MenuDetails).Where(s => s.Status == 0).OrderBy(o => o.MenuDetails.OrderBy(b=>b.Sorting).FirstOrDefault().Sorting ).ToListAsync();
order the Menus by MenuDetails.sorting
i want to order by Menu.Sorting and MenuDetails.Sorting
Tuesday, July 23, 2019 8:31 AM -
Hi Mark,
Does it mean that OrderBy with more than one field? If so, you can try "ThenBy".
Here is the document you can refer to.
Regards,
Kyle
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.Wednesday, July 24, 2019 7:15 AM -
Hi Kyle
there is 2 order 1st is in the main class the 2nd order is in the included class
Wednesday, July 24, 2019 11:54 AM -
Hi Mark,
Does it mean first orderby "menu.sorting" then orderby "menudetail.sorting" in each menu?
public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Menu m1 = new Menu { Sorting = 1, MenuDetails = new List<MenuDetail> { new MenuDetail { Sorting = 1 }, new MenuDetail { Sorting = 2 } } }; Menu m2 = new Menu { Sorting = 3, MenuDetails = new List<MenuDetail> { new MenuDetail { Sorting = 1 }, new MenuDetail { Sorting = 2 }, new MenuDetail { Sorting = 1 } } }; Menu m3 = new Menu { Sorting = 2, MenuDetails = new List<MenuDetail> { new MenuDetail { Sorting = 2 }, new MenuDetail { Sorting = 1 } } }; List<Menu> menus = new List<Menu>(); menus.Add(m1); menus.Add(m2); menus.Add(m3); var result = menus.OrderBy(x => x.Sorting).ToList(); foreach (var i in result) { var query = i.MenuDetails.OrderBy(x => x.Sorting).ToList(); i.MenuDetails = query; } } } public class Menu { public int Sorting { get; set; } public List<MenuDetail> MenuDetails { get; set; } } public class MenuDetail { public int Sorting { get; set; } }
Regards,
Kyle
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.Thursday, July 25, 2019 9:59 AM