最近、それぞれが相互に関連付けられているページとナビゲーションメニューエントリのリストを並べ替える必要がありました。
それぞれNavigation
にPage
プロパティがあります。それぞれPage
にNavigation
プロパティがあります。それらは私のデータベースの外部キー参照です。
アイテムのNavigation
リストとすべてのPage
アイテムのリストがあります。問題は、Page
に関連付けられているかどうかに関係なく、アイテムNavigation
のリストに保存されることです。Page
Page
次のように並べ替えられたアイテムのリストを作成したいと思います。null以外のアイテムNavigation
は、プロパティで並べ替えられPage.Navigation.Index
ます。nullのあるアイテムは、プロパティ、次にプロパティでNavigation
並べ替えられます。Page.Title
Page.ID
以下は私たちが現在行っていることであり、いくつかの例外を除いて、ほとんどの部分で機能します。私がこれに関して抱えている問題は、ナビゲーションが関連付けられていないページの重複したタイトルを処理しないことです。
List<Page> page1 = db.Navigations.OrderBy(n => n.Index).Select(n => n.Page).ToList();
List<Page> page2 = db.Pages.Where(p => !db.Navigations.Contains(p.Navigation)).ToList();
model.Pages = page1.Concat(page2).ToList();
これがいくつかのサンプルデータと期待される結果です
Pages Table (PageID, Title, Content)
0, "Home", "<html>This is a home page</html>"
3, "Some Page", "<html>This is some page.</html>"
2, "Some hidden page", "<html>This is some hidden page.</html>"
4, "Products", "<html>We've got products!</html>"
5, "aaaaa", "<html>This should be sorted to the top of pages with no nav</html>"
Navigations Table (PageID, Index)
0, 0
3, 2
4, 1
Output (PageID, Title, Content)
0, "Home", "<html>This is a home page</html>"
4, "Products", "<html>We've got products!</html>"
3, "Some Page", "<html>This is some page</html>"
5, "aaaaa", "<html>This should be sorted to the top of pages with no nav</html>"
2, "Some hidden page", "<html>This is some hidden page.</html"
これが見栄えの良い方法で、また手続き型構文の代わりにクエリ構文で実行できるかどうか知りたいです。