将平面集合转换为一个?多个 C#/Linq
Convert flat collection to one-?many C#/Linq
我有一个数据集在这个class的集合中:
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
目标是将其纳入本class的集合:
public class ClientInfo
{
public int boclient_id { get; set; }
public string beneficialownertype_name { get; set; }
public List<AccountInfo> Accounts { get; set; }
}
与这个class是一对多的关系:
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
变量重新用作 aoCLientData
项和分组项。在你的内部 select 使用 .Select(a => new AccountInfo...
然后使用 a
而不是 c
到 select a.account_id
、a.fi_name
和 a.acct_number
.
var result = data
.GroupBy(x => new { x.boclient_id, x.beneficialownertype_name })
.Select(x =>
new ClientInfo
{
boclient_id = x.Key.boclient_id,
beneficialownertype_name = x.Key.beneficialownertype_name,
Accounts = x.Select(a =>
new AccountInfo
{
account_id = a.account_id,
fi_name = a.fi_name,
acct_number = a.acct_number
}).ToList()
});
如果我查看您的代码,我不明白为什么您会得到多个 ClientInfos
,每个 属性 Accounts
的值都为 null
。我什至不希望有一个空的帐户列表。
您是否尝试过使用带有参数 resultSelector 的 overload of GroupBy?
List<ClientInfo> clientInfo = aoClientData
.GroupBy(
// parameter keySelector:
clientInfo => new
{
BoClientId = clientInfo.boclient_id,
BeneficialOwnerTypeName = clientInfo.beneficialownertype_name,
})
// parameter resultSelector: for every key item with all its zero or more AOClientData
// make one new ClientInfo
(key, aoClientsWithThisKey) => new ClientInfo
{
boclient_id = key.BoClientId,
beneficialownertype_name = key.BeneficialOwnerTypeName,
Accounts = aoClientsWithThisKey.Select(aoClient => new AccountInfo
{
account_id = aoClient.account_id,
fi_name = aoClient.fi_name,
acct_number = aoClient.acct_number
})
.ToList(),
})
.ToList();
解决方法如下:
var items = aoClientData.GroupBy(c => new { c.boclient_id, c.beneficialownertype_name })
.Select(grp => new ClientInfo()
{
boclient_id = grp.Key.boclient_id,
beneficialownertype_name = grp.Key.beneficialownertype_name,
Accounts = grp.Select(r => new AccountInfo()
{
account_id = r.account_id,
acct_number = r.acct_number
}).ToList()
}).ToList();
我有一个数据集在这个class的集合中:
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
目标是将其纳入本class的集合:
public class ClientInfo
{
public int boclient_id { get; set; }
public string beneficialownertype_name { get; set; }
public List<AccountInfo> Accounts { get; set; }
}
与这个class是一对多的关系:
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
变量重新用作 aoCLientData
项和分组项。在你的内部 select 使用 .Select(a => new AccountInfo...
然后使用 a
而不是 c
到 select a.account_id
、a.fi_name
和 a.acct_number
.
var result = data
.GroupBy(x => new { x.boclient_id, x.beneficialownertype_name })
.Select(x =>
new ClientInfo
{
boclient_id = x.Key.boclient_id,
beneficialownertype_name = x.Key.beneficialownertype_name,
Accounts = x.Select(a =>
new AccountInfo
{
account_id = a.account_id,
fi_name = a.fi_name,
acct_number = a.acct_number
}).ToList()
});
如果我查看您的代码,我不明白为什么您会得到多个 ClientInfos
,每个 属性 Accounts
的值都为 null
。我什至不希望有一个空的帐户列表。
您是否尝试过使用带有参数 resultSelector 的 overload of GroupBy?
List<ClientInfo> clientInfo = aoClientData
.GroupBy(
// parameter keySelector:
clientInfo => new
{
BoClientId = clientInfo.boclient_id,
BeneficialOwnerTypeName = clientInfo.beneficialownertype_name,
})
// parameter resultSelector: for every key item with all its zero or more AOClientData
// make one new ClientInfo
(key, aoClientsWithThisKey) => new ClientInfo
{
boclient_id = key.BoClientId,
beneficialownertype_name = key.BeneficialOwnerTypeName,
Accounts = aoClientsWithThisKey.Select(aoClient => new AccountInfo
{
account_id = aoClient.account_id,
fi_name = aoClient.fi_name,
acct_number = aoClient.acct_number
})
.ToList(),
})
.ToList();
解决方法如下:
var items = aoClientData.GroupBy(c => new { c.boclient_id, c.beneficialownertype_name })
.Select(grp => new ClientInfo()
{
boclient_id = grp.Key.boclient_id,
beneficialownertype_name = grp.Key.beneficialownertype_name,
Accounts = grp.Select(r => new AccountInfo()
{
account_id = r.account_id,
acct_number = r.acct_number
}).ToList()
}).ToList();