我有一個從我們的API返回的具有以下數據結構的帳戶數組。根據內部數組上的對象的屬性對數組進行排序
Accounts[] accounts = [
{
AccountNumber: 0000000001,
PrimaryPerson: Virginia,
PrimaryPersonNumber: 0001,
PersonRoleCode: "OWN",
PersonRoleDescription: "Owner",
RoleOrder: 1
PersonRoles: [
{
AccountRoleCode: "CO",
AccountRoleDescription: "Co-Owner",
PersonName: "David",
PersonNumber: 0002,
RoleOrder: 2
},
{
AccountRoleCode: "POA",
AccountRoleDescription: "Power of Attorney",
PersonName: "Clark",
PersonNumber: 0003,
RoleOrder: 6
}
]
},
{
AccountNumber: 0000000002,
PrimaryPerson: Clark,
PrimaryPersonNumber: 0003,
PersonRoleCode: "OWN",
PersonRoleDescription: "Owner",
RoleOrder: 1
PersonRoles: [
{
AccountRoleCode: "CO",
AccountRoleDescription: "Co-Owner",
PersonName: "Virginia",
PersonNumber: 0001,
RoleOrder: 2
},
{
AccountRoleCode: "POA",
AccountRoleDescription: "Power of Attorney",
PersonName: "David",
PersonNumber: 0002,
RoleOrder: 6
}
]
},
{
AccountNumber: 0000000003,
PrimaryPerson: David,
PrimaryPersonNumber: 0002,
PersonRoleCode: "OWN",
PersonRoleDescription: "Owner",
RoleOrder: 1
PersonRoles: [
{
AccountRoleCode: "CO",
AccountRoleDescription: "Co-Owner",
PersonName: "Clark",
PersonNumber: 0003,
RoleOrder: 2
},
{
AccountRoleCode: "CO",
AccountRoleDescription: "Co-Owner",
PersonName: "Virginia",
PersonNumber: 0001,
RoleOrder: 2
},
{
AccountRoleCode: "POA",
AccountRoleDescription: "Power of Attorney",
PersonName: "Virginia",
PersonNumber: 0001,
RoleOrder: 6
}
]
}
];
我需要梳理基礎上,RoleOrder
爲Accounts
對象本身這個Accounts
對象和RoleOrder
在每個PersonRoles[]
指數PersonRole
對象。
我試過用LINQ來做,但我不確定它爲什麼不能正常工作。
下面是我試過的和我期望它做的事情。
IEnumberable<Account> sortedAccounts = accounts
.OrderBy(acct => acct.PersonRoles
.Where(role => role.PersNbr == 0001)
.Select(x => x.RoleOrder)).ToList();
我預期它由PersonRoles[]
其中PersNbr
是等於給定PersNbr(0001
)訂購的主accounts[]
和排序在RoleOrder
。
所以基本上我想根據RoleOrder
爲一個特定的人排序accounts[]
。
所以排序基於Virginia
將導致Accounts[] accounts = [0000000001, 0000000003, 0000000002]
排序基於David
將導致Accounts[] accounts = [0000000003, 0000000001, 0000000002]
我有LINQ沒有做任何事情(順序保持不變),這讓我覺得我正在按照正確的順序執行查詢。
如何編寫LINQ查詢來執行此操作,還是需要將它分解爲不同的查詢?
這不能提供問題的答案。一旦你有足夠的[聲譽](https://stackoverflow.com/help/whats-reputation),你將可以[對任何帖子發表評論](https://stackoverflow.com/help/privileges/comment);相反,[提供不需要提問者澄清的答案](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can- I-DO-代替)。 - [來自評論](/ review/low-quality-posts/18365520) – Rabbid76