按 ID 过滤对象集合
自学一些 C#,我的问题是我有一个类,它创建 Person 对象并将它们添加到列表 (myPersonList) 中。我还有一个 PartyGroup 类,它将 PersonList 作为其属性之一。我的问题是 myPersonList 包含整个结果集,因此目前每次创建新的 PartyGroup 时都会添加 myPersonList 中的所有值。有没有办法告诉它只在 myPersonList 中添加那些值,其中 Person.PartyGroupId 与我们正在处理的当前 PartyGroup groupID 匹配。希望这是有道理的。谢谢。
//人
using (AseDataReader reader = commandPerson.ExecuteReader())
{
while (reader.Read())
{
Person person = new Person();
person.PersonID = Convert.ToInt32(reader["person_id"]);
person.PartyGroupID = Convert.ToInt32(reader["party_group_id"]);
person.FullName = reader["full_name"].ToString();
myPersonList.Add(person);
}
}
//党组
using (AseDataReader reader = commandPartyGroup.ExecuteReader())
{
while (reader.Read())
{
int groupId = Convert.ToInt32(reader["party_group_id"]);
if (myPartyGroupList.Where(partyGroup => partyGroup.PartyGroupID == groupId).Any() == false)
{
PartyGroup partyGroup = new PartyGroup()
{
PartyGroupID = groupId,
PartyGroupName = reader["party_group_name"].ToString(),
PersonList = myPersonList//where PersonList objects contain this groupId???
};
myPartyGroupList.Add(partyGroup);
}
}
}
Teaching myself some c# and my problem is that I have a class which creates Person objects and adds them to a List (myPersonList). I also have a PartyGroup class which takes a PersonList as one of its properties. My issue is that myPersonList contains the whole results set so at the moment each time it creates a new PartyGroup it adds all the values in myPersonList. Is there a way to tell it to only add those values in myPersonList where the Person.PartyGroupId matches the current PartyGroup groupID we are dealing with. Hope that makes sense. Thanks.
//Person
using (AseDataReader reader = commandPerson.ExecuteReader())
{
while (reader.Read())
{
Person person = new Person();
person.PersonID = Convert.ToInt32(reader["person_id"]);
person.PartyGroupID = Convert.ToInt32(reader["party_group_id"]);
person.FullName = reader["full_name"].ToString();
myPersonList.Add(person);
}
}
//PartyGroup
using (AseDataReader reader = commandPartyGroup.ExecuteReader())
{
while (reader.Read())
{
int groupId = Convert.ToInt32(reader["party_group_id"]);
if (myPartyGroupList.Where(partyGroup => partyGroup.PartyGroupID == groupId).Any() == false)
{
PartyGroup partyGroup = new PartyGroup()
{
PartyGroupID = groupId,
PartyGroupName = reader["party_group_name"].ToString(),
PersonList = myPersonList//where PersonList objects contain this groupId???
};
myPartyGroupList.Add(partyGroup);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我正确理解你的问题,你只需要:
If I understand your problem correctly, you just need: