2017-12-27 90 views
0

我有一個從我們的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 
         } 
        ] 
    } 
]; 

我需要梳理基礎上,RoleOrderAccounts對象本身這個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查詢來執行此操作,還是需要將它分解爲不同的查詢?

回答

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(); 

說明: 我們從「選擇」的聲明開始,我們在其中重新排序內每個PersonRoles陣列的內部結構帳戶,這種方式更清晰,將有助於執行賬戶訂購簡單一點

後來,根據同樣的邏輯

注意我們訂購帳戶陣列的istead的「返回999「語句,您可以編寫一些其他邏輯來處理PersonRoles數組內沒有相關用戶的情況。

+0

這不能提供問題的答案。一旦你有足夠的[聲譽](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-c​​an- I-DO-代替)。 - [來自評論](/ review/low-quality-posts/18365520) – Rabbid76

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); 
相關問題