LINQ to Object - 如何为子组实现 WHERE 子句“如果至少有一个元素是”

LINQ to Object - How to implement WHERE clause `If at least one of the elements is` for sub-group

我有一种无序记录:

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; }
}

并对按客户字段分组的行进行 select 投影:

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 子句?

例如:

• 逻辑的 where 子句如何:get clients, all of whose accounts is open 得到这个?:

+------+------------------+
| Code | Accounts         |
|      +---------+--------+
|      | Number  | Status |
+------+---------+--------+
| 123  | 123.def | Open   |
|      +---------+--------+
|      | 123.abc | Open   |
+------+---------+--------+

• 逻辑的 where 子句如何: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   |
+------+---------+--------+

完全互动 code listing 在 dotnetfiddle

查询:获取客户,所有客户的账户都是开放的

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.

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