【发布时间】:2022-01-10 00:46:00
【问题描述】:
我有一个数据集在这个类的集合中:
public class AOClientData
{
public int boclient_id { get; set; }
public string beneficialownertype_name { get; set; }
public int account_id { get; set; }
public string fi_name { get; set; }
public string acct_number { get; set; }
}
看起来像这样:
boclient_id beneficialownertype_name account_id fi_name acct_number
1001 Joe 501 ABC 12345
1001 Joe 502 BCA 54321
1002 Fred 990 DDd 22334
目标是把它放入这个类的集合中:
public class ClientInfo
{
public int boclient_id { get; set; }
public string beneficialownertype_name { get; set; }
public List<AccountInfo> Accounts { get; set; }
}
与这个类是一对多的关系:
public class AccountInfo
{
public int account_id { get; set; }
public string fi_name { get; set; }
public string acct_number { get; set; }
}
结果应该是如下所示的一组 ClientInfo 对象:
1001 Joe {501, ABC, 12345 },
{502, BCA, 54321 }
1002 Fred {990, DDd, 22334 }
这是我的尝试,它确实加载了所有客户端数据,但 ClientInfo.Accounts 属性中 AccountInfo 对象的属性全部为空:
List<ClientInfo> clientInfo = aoClientData
.GroupBy(c => new { c.boclient_id, c.beneficialownertype_name })
.Select(xGrp => new ClientInfo
{
boclient_id = xGrp.Key.boclient_id,
beneficialownertype_name = xGrp.Key.beneficialownertype_name,
Accounts = xGrp
.Select(c => new AccountInfo
{
account_id = c.account_id,
fi_name = c.fi_name,
acct_number = c.acct_number
})
.ToList()
})
.ToList();
LINQ 出了什么问题?
【问题讨论】:
标签: c# linq collections visual-studio-2019