我知道这个标题有点平淡无奇。我有3个班级(用户,案例,办公室)。用户和案例包含其中的办事处列表。我需要比较用户和案例中的Office列表,如果办公室的ID匹配,我需要将案例中的ID添加到用户。因此,最终目标是让Users类拥有与Cases类中的Office相匹配的任何主管局。
为了增加清晰度,我希望比较两个officeLists(users.officeList和cases.OfficeList),当任何Cases.OfficeList.ID == Users.OfficeList.ID时,我需要将Case.ID添加到Users.caseAdminIDList列表
有什么想法吗?
我的代码(不起作用)
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
//启动我的数据类
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 Cases
{
public Cases()
{
List<Offices> officeList = new List<Offices>();
ID = 0;
}//end constructor
public int ID { get; set; }
public string name { get; set; }
public int adminID { get; set; }
public bool alreadyInDB { get; set; }
public bool deleted { get; set; }
public List<Offices> officeList { get; set; }
}
//在我的主要方法
中 private bool grabCasesFromAdminDB() //gets cases from DB1 (AdminDB)
{
DatabaseIO dbIO = new DatabaseIO();
caseList = dbIO.getCasesFromAdminDB(adminDBConString, caseList).ToList();
if (dbIO.errorMessage == null || dbIO.errorMessage.Equals(""))
{
if (getCaseOfficeRelationship())
return true;
else
return false;
}//end if
else
{
MessageBox.Show(dbIO.errorMessage, "Admin DB Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}//end else
}//end method
private bool grabCasesFromListDB()//grabs cases from the main db
{
DatabaseIO dbIO = new DatabaseIO();
caseList = dbIO.getCasesFromMainDB(connectionString, caseList).ToList();
if (dbIO.errorMessage == null || dbIO.errorMessage.Equals(""))
return true;
else
{
MessageBox.Show(dbIO.errorMessage, "Main DB Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}//end else
}//end method
private bool getCaseOfficeRelationship()//adds office relationship to cases
{
DatabaseIO dbIO = new DatabaseIO();
caseList = dbIO.getOfficeToCaseRelationship(connectionString, caseList).ToList();
if (dbIO.errorMessage == null || dbIO.errorMessage.Equals(""))
{
if (getOfficeNamesByID())
return true;
else
return false;
}//end if
else
{
MessageBox.Show(dbIO.errorMessage, "Cases To Offices Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}//end else
}//end method
private bool getOfficeNamesByID()//grabs the id of the offices by name
{
List<Offices> officeList = new List<Offices>();
DatabaseIO dbIO = new DatabaseIO();
officeList = dbIO.getOfficeNamesByOfficeID(connectionString).ToList();
if (dbIO.errorMessage == null || dbIO.errorMessage.Equals(""))
{
matchOfficeNamesToIDs(officeList);
return true;
}//end if
else
{
MessageBox.Show(dbIO.errorMessage, "Getting Office List Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}//end else
}//end method
private void matchOfficeNamesToIDs(List<Offices> officeList)
{
foreach (Cases cases in caseList)
if (cases.officeList != null)
foreach (Offices office in cases.officeList)
{
foreach (Offices innerOffice in officeList)
if (innerOffice.ID == office.ID)
office.name = innerOffice.name;
}//end foreach
}//end method
//我的DBIO类的一个例子
public List<Cases> getCasesFromAdminDB(string adminDBConString, List<Cases> caseList)
{
try
{
Cases cases = null;
SqlConnection con = new SqlConnection();
con.ConnectionString = adminDBConString;
con.Open();
SqlCommand command = new SqlCommand();
command.Connection = con;
command.CommandText = "select CS_Case_ID, Case_Name from CS_Case where CS_Product_Type_ID = 2 and CS_Case_Status_ID = 1";
SqlDataReader thisReader = command.ExecuteReader();
int idxID = thisReader.GetOrdinal("CS_Case_ID");
int idxName = thisReader.GetOrdinal("Case_Name");
while (thisReader.Read())
{
bool found = false;
foreach (Cases tempCase in caseList)
{
if (tempCase.adminID == Int32.Parse(thisReader.GetValue(idxID).ToString()))
{
tempCase.alreadyInDB = true;
found = true;
}//end if
}//end foreach
if (!found)
{
cases = new Cases();
cases.adminID = Int32.Parse(thisReader.GetValue(idxID).ToString());
cases.name = thisReader.GetValue(idxName).ToString();
cases.alreadyInDB = false;
cases.officeList = new List<Offices>();
caseList.Add(cases);
}//end if
}//end while
thisReader.Close();
return caseList;
}//end try
catch (Exception excep1)
{
errorMessage = "Cases could not be loaded from the Admin Console." + "\r\n" + "Error message: " + excep1.ToString();
return caseList;
}//end catch
}//end method
答案 0 :(得分:2)
这样的复杂查询最好由LINQ处理。如果您在Users
和Cases
上都有共同元素,那么这将是join
的工作。但是在这种情况下,它们不是通用元素,而是包含列表,并且您希望“连接”两个列表具有公共元素的列表。
那么,首先,为用户包含特定案例的条件是什么?
case.officeList.Any(caseOffice => user.officeList.Any(userOffice => caseOffice.ID == userOffice.ID))
即。案例的officeList中的任何办公室都包含在用户的officeList
。
现在我们有了这个条件,我们可以在LINQ Where
子句中使用它来提取所有需要的案例:
caseList.Where(case =>
case.officeList.Any(caseOffice => user.officeList.Any(userOffice => caseOffice.ID == userOffice.ID)))
返回我们的案例集合,所以最后我们只想Select
我们需要的案例部分,即adminID。
caseList.Where(case =>
case.officeList.Any(caseOffice => user.officeList.Any(userOffice => caseOffice.ID == userOffice.ID)))
.Select(case => case.adminID);
所以把它们放在一起:
foreach(Users users in userList)
{
users.caseAdminIDList = caseList.Where(case =>
case.officeList.Any(caseOffice => user.officeList.Any(userOffice => caseOffice.ID == userOffice.ID)))
.Select(case => case.adminID).ToList();
}
答案 1 :(得分:0)
public Cases()
{
List<Offices> officeList = new List<Offices>();
ID = 0;
}//end constructor
这是一个名为officeList
的新变量。它不会在构造函数外部使用officeList
。
这需要更改为:
officeList = new List<Offices>();
这将正确初始化类字段。
答案 2 :(得分:0)
您可以像这样实施您的办公室课程:
class Offices:IEquatable<Offices>
{
public int ID { get; set; }
public string name { get; set; }
public bool Equals(Offices other)
{
return this.ID.Equals(other.ID);
}
public override int GetHashCode()
{
return this.ID.GetHashCode();
}
}
然后在你的代码中让Users类有任何与Cases类中的办公室相匹配的办公室,只需这样做;
usersVariable.CaseAdminIDList = usersVariable.OfficeList.Intersect(casesVariable.OfficeList).Select(o => o.ID).ToList();
同样在您的用户和案例类中,您需要正确初始化列表(您使用自动属性并在ctor中声明并初始化一个新变量),某些内容 比如这样:
class Users
{
public Users()
{
officeList = new List<Offices>();
caseAdminIDList = new List<int>();
ID = 0;
}//end constructor
private List<Offices> officeList;
public List<Offices> OfficeList
{
get { return officeList; }
set { officeList = value; }
}
private List<int> caseAdminIDList;
public List<int> CaseAdminIDList
{
get { return caseAdminIDList; }
set { caseAdminIDList = value; }
}
//other members.....
}