【发布时间】:2017-12-19 21:46:28
【问题描述】:
如何对需要在PersonRoles 数组中排序的Accounts 数组进行排序,该数组说明关联人员在该帐户中的角色?
例如,
Bob 是帐户 12 的所有者 (O)、帐户 123 的共同签名人 (CO) 以及帐户 1234 的受益人。
Joe 是帐户 123 的所有者 (O),帐户 1234 是帐户 12 的受益人 (BE)。
我如何将Accounts for Bob 的数组按所有者 (O) 的顺序排序,然后是共同签名者 ('CO'),然后是受益人 (BE ) 按此顺序。
账户对象结构
Accounts
{
AccountNumber: 12,
PersonRoles: [
{
AccountRoleCode: "O",
AccountRoleDescription: "Owner",
Person: "Bob"
},
{
AccountRoleCode: "CO",
AccountRoleDescription: "Co-Signer",
Person: ""
},
{
AccountRoleCode: "BE",
AccountRoleDescription: "Beneficiary",
Person: "Joe"
},
],
Balance: 5.00
},
{
AccountNumber: 123,
PersonRoles: [
{
AccountRoleCode: "O",
AccountRoleDescription: "Owner",
Person: "Joe"
},
{
AccountRoleCode: "CO",
AccountRoleDescription: "Co-Signer",
Person: "Bob"
},
{
AccountRoleCode: "BE",
AccountRoleDescription: "Beneficiary",
Person: null
},
],
Balance: 100.00
},
{
AccountNumber: 1234,
PersonRoles: [
{
AccountRoleCode: "O",
AccountRoleDescription: "Owner",
Person: "Joe"
},
{
AccountRoleCode: "CO",
AccountRoleDescription: "Co-Signer",
Person: null
},
{
AccountRoleCode: "BE",
AccountRoleDescription: "Beneficiary",
Person: "Bob"
},
],
Balance: 10000000.00
}
从 API 返回的 Bob 下列出的原始帐户数组。
[1234, 12, 123]
所需的排序数组。
[12, 123, 1234]
我最初的方法是在数组上使用 LINQ,但我不确定如何循环遍历 Accounts[],然后循环遍历 PersonRoles[] 以根据 PersonRoles[] 对 Accounts[] 进行排序。
这需要双 LINQ 查询吗?还是另一种方法会更好?
【问题讨论】:
-
使用 Array.Sort(Array,IComparer ): msdn.microsoft.com/en-us/library/aw9s5t8f(v=vs.110).aspx