Combining lists of objects containing lists of objects in c#
Posted
by
Dan H
on Stack Overflow
See other posts from Stack Overflow
or by Dan H
Published on 2014-06-09T15:21:36Z
Indexed on
2014/06/09
15:24 UTC
Read the original article
Hit count: 378
The title is a little prosaic, I know. I have 3 classes (Users, Cases, Offices). Users and Cases contain a list of Offices inside of them. I need to compare the Office lists from Users and Cases and if the ID's of Offices match, I need to add those IDs from Cases to the Users. So the end goal is to have the Users class have any Offices that match the Offices in the Cases class. Any ideas?
My code (which isnt working)
foreach (Users users in userList)
foreach (Cases cases in caseList)
foreach (Offices userOffice in users.officeList)
foreach (Offices caseOffice in cases.officeList)
{
if (userOffice.ID == caseOffice.ID)
users.caseAdminIDList.Add(cases.adminID);
}//end foreach
//start my data classes
class Users
{
public Users()
{
List<Offices> officeList = new List<Offices>();
List<int> caseAdminIDList = new List<int>();
ID = 0;
}//end constructor
public int ID { get; set; }
public string name { get; set; }
public int adminID { get; set; }
public string ADuserName { get; set; }
public bool alreadyInDB { get; set; }
public bool alreadyInAdminDB { get; set; }
public bool deleted { get; set; }
public List<int> caseAdminIDList { get; set; }
public List<Offices> officeList { get; set; }
}
class Offices
{
public int ID { get; set; }
public string name { get; set; }
}
class Users
{
public Users()
{
List<Offices> officeList = new List<Offices>();
List<int> caseAdminIDList = new List<int>();
ID = 0;
}//end constructor
public int ID { get; set; }
public string name { get; set; }
public int adminID { get; set; }
public string ADuserName { get; set; }
public bool alreadyInDB { get; set; }
public bool alreadyInAdminDB { get; set; }
public bool deleted { get; set; }
public List<int> caseAdminIDList { get; set; }
public List<Offices> officeList { get; set; }
}
© Stack Overflow or respective owner