根据内部数组上的对象的属性对数组进行排序
我有一个从我们的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查询来执行此操作,还是需要将它分解为不同的查询?
回答:
我被要求提供一个完整的答案,在这里我们去:
首先,我想提请你注意“设计洞”已准备好:如果你不具备,会发生什么这个“人”呢?对于这种情况,我已经允许自己假设所有这些结果都会在最后显示出来。
关于问题:问题的复杂性来自您需要执行两种不同的排序:在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();
说明: 我们从“选择”的声明开始,我们在其中重新排序内每个PersonRoles阵列的内部结构帐户,这种方式更清晰,将有助于执行账户订购简单一点
后来,根据同样的逻辑
注意我们订购帐户阵列的istead的“返回999“语句,您可以编写一些其他逻辑来处理PersonRoles数组内没有相关用户的情况。
回答:
请参见: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);
以上是 根据内部数组上的对象的属性对数组进行排序 的全部内容, 来源链接: utcz.com/qa/266709.html