我有一个从我们的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