我有一種無序記錄:
public class Row {
public string Client { get; set; }
public string Account { get; set; }
public string Status { get; set; }
}
它的初始化是:
List<Row> accounts = new List<Row>() {
new Row() { Client = "123", Account = "123.def", Status = "Open" },
new Row() { Client = "456", Account = "456.def", Status = "Closed" },
new Row() { Client = "123", Account = "123.abc", Status = "Open" },
new Row() { Client = "789", Account = "789.abc", Status = "Open" },
new Row() { Client = "456", Account = "456.abc", Status = "Closed" },
new Row() { Client = "789", Account = "789.ghi", Status = "Open" },
new Row() { Client = "789", Account = "789.def", Status = "Closed" },
new Row() { Client = "789", Account = "789.jkl", Status = "Open" },
};
輸出是:
-------- --------- --------
| Client | Account | Status |
-------- --------- --------
| 123 | 123.def | Open |
-------- --------- --------
| 456 | 456.def | Closed |
-------- --------- --------
| 123 | 123.abc | Open |
-------- --------- --------
| 789 | 789.abc | Open |
-------- --------- --------
| 456 | 456.abc | Closed |
-------- --------- --------
| 789 | 789.ghi | Open |
-------- --------- --------
| 789 | 789.def | Closed |
-------- --------- --------
| 789 | 789.jkl | Open |
-------- --------- --------
之后,為了進一步操作物件,我輸入了以下附加型別:
public class Client {
public string Code { get; set; }
public List<Account> Accounts { get; set; }
}
public class Account {
public string Number { get; set; }
public string Status { get; set; }
}
并對按客戶欄位分組的行進行選擇投影:
List<Client> clients = accounts
.GroupBy(x => x.Client)
.Select(y => new Client() {
Code = y.Key,
Accounts = y.GroupBy(z => z.Account)
.Select(z => new Account() {
Number = z.First().Account,
Status = z.First().Status
}).ToList()
}).ToList();
我得到輸出:
------ ------------------
| Code | Accounts |
| --------- --------
| | Number | Status |
------ --------- --------
| 123 | 123.def | Open |
| --------- --------
| | 123.abc | Open |
------ --------- --------
| 456 | 456.def | Closed |
| --------- --------
| | 456.abc | Closed |
------ --------- --------
| 789 | 789.abc | Open |
| --------- --------
| | 789.ghi | Open |
| --------- --------
| | 789.def | Closed |
| --------- --------
| | 789.jkl | Open |
------ --------- --------
但是,我的問題是:如何為過濾的Accounts子組實作 WHERE 子句?
例如:
? How where-clause for logic:get clients, all of whose accounts is open為了得到這個?:
------ ------------------
| Code | Accounts |
| --------- --------
| | Number | Status |
------ --------- --------
| 123 | 123.def | Open |
| --------- --------
| | 123.abc | Open |
------ --------- --------
? How where-clause for logic:get clients, who have at least one account is open?為了得到這個?:
------ ------------------
| Code | Accounts |
| --------- --------
| | Number | Status |
------ --------- --------
| 123 | 123.def | Open |
| --------- --------
| | 123.abc | Open |
------ --------- --------
| 789 | 789.abc | Open |
| --------- --------
| | 789.ghi | Open |
| --------- --------
| | 789.def | Closed |
| --------- --------
| | 789.jkl | Open |
------ --------- --------
dotnetfiddle 上的完整互動式代碼清單
uj5u.com熱心網友回復:
查詢:獲取所有帳戶都已打開的客戶
var ClientsWithAllAccountsAsOpen = clients
.Where(c => c.Accounts
.All(a => a.Status == "Open"));
// Add ToList() or equivalent if you need to materialise.
查詢:獲取至少有一個帳戶已打開的客戶
var ClientsWithAtLeastOneOpenAccounts = clients
.Where(c => c.Accounts
.Any(a => a.Status == "Open"));
// Add ToList() or equivalent if you need to materialise.
uj5u.com熱心網友回復:
get clients, all of whose accounts is open:
var openAccountsByClient = accounts
.Where(e => e.Status == "Open")
.GroupBy(x => x.Client)
.Select(y => new Client() {
...
}).ToList();
get clients, who have at least one account is open?:
var accountsByClientsWithOneAccount = accounts
.GroupBy(x => x.Client)
.Where(x => x.Any(e => e.Status == "Open"))
.Select(y => new Client() {
...
}).ToList();
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/344921.html
