我有一系列从我们的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
}
]
}
];
我需要根据Accounts
对象本身的RoleOrder
和每个Accounts
对象的RoleOrder
对此PersonRole
对象进行排序{1}}索引。
我已尝试使用LINQ,但我不确定为什么它无法正常工作。
这是我尝试过的以及我期望它做的事情。
PersonRoles[]
我希望它可以在IEnumberable<Account> sortedAccounts = accounts
.OrderBy(acct => acct.PersonRoles
.Where(role => role.PersNbr == 0001)
.Select(x => x.RoleOrder)).ToList();
的{{1}}等于给定的PersNbr(accounts[]
)的位置对主PersonRoles[]
进行排序,并对{{1}进行排序}}
所以基本上我只想根据PersNbr
对特定人员排序0001
。
因此,基于RoleOrder
的排序将导致accounts[]
基于RoleOrder
的排序将导致Virginia
我对LINQ所做的事情并没有做任何事情(订单保持不变),这让我觉得我正在以正确的顺序进行查询。
如何编写LINQ查询来执行此操作,还是需要将其拆分为不同的查询?
答案 0 :(得分:1)
我被要求提供完整的答案,我们走了:
首先,我想提请您注意一个“设计漏洞”:如果您根本没有这个“人”,会发生什么?对于这种情况,我允许自己假设所有这些结果将在最后显示。
关于问题:问题的复杂性来自于您需要执行两个不同的排序:在Account []数组排序期间,以及每个帐户内的PersonRoles数组内,而两个排序具有相同的逻辑。
假设你的对象是可变的,我建议下一个解决方案:
IEnumerable<Account> sortedAccounts = accounts
.Select(x => {
x.PersonRoles = x
.PersonRoles
.OrderBy(acct => {
if (acct.PersonNumber != 001)
return 999;
return acct.RoleOrder;
}).ToArray();
return x;
})
.OrderBy(acct => {
var relevantRoles = acct.PersonRoles
.Where(role => role.PersonNumber == 0001)
.Select(x => x.RoleOrder);
if (relevantRoles.Any() == false)
return 999;
return relevantRoles.Max();
}
).ToList();
说明: 我们从“select”语句开始,在其中我们重新排序每个帐户内PersonRoles数组的内部结构,这样它更清晰,并有助于执行更简单的帐户排序
稍后,我们按照相同的逻辑
订购帐户数组请注意,除了“返回999”语句之外,您还可以编写一些其他逻辑来解决PersonRoles数组中没有相关用户的情况。
答案 1 :(得分:1)
请参阅:Sort a list and all its nested objects using Linq
您可以在一个声明中完成。在每个LINQ内部,您需要使用外部语句中的信息创建新对象。
class Account
{
public int AccountNumber { get; set; }
public Role[] Roles { get; set; }
}
class Role
{
public string State { get; set; }
public int RoleOrder { get; set; }
}
Account[] accounts = new Account[3];
accounts[0] = new Account { AccountNumber = 1, Roles = new
Role[2] { new Role { State = "AK", RoleOrder = 2 }, new Role { State =
"CO", RoleOrder = 1 } } };
accounts[1] = new Account { AccountNumber = 3, Roles = new
Role[2] { new Role { State = "NY", RoleOrder = 66 }, new Role { State =
"FL", RoleOrder = 5 } } };
accounts[2] = new Account { AccountNumber = 2, Roles = new
Role[2] { new Role { State = "CA", RoleOrder = 9 }, new Role { State =
"AZ", RoleOrder = 8 } } };
IEnumerable<Account> info = accounts
.Select(x => new Account {
AccountNumber = x.AccountNumber
, Roles = x.Roles.OrderBy(y => y.RoleOrder).ToArray()
}
).Where(x => x.AccountNumber == 2);