问题描述
大家好.
我是OOP的新手,正在尝试使用C#建立类关联.我有两个班级,分别是部门和员工.
我想让所有员工都集中在一个部门中,而在另一部门中.我已经在Google上搜索过,但很不幸.我不确定自己的做法是对还是错.
任何帮助,将不胜感激.请查看代码.在此先感谢
- - - - - - - - - - - - - -部门 - - - - - - - - - - - ----
Hi Everyone.
I am new to OOP and trying to make class association in C#. I have two classes called Department and Employee.
I want to get all the employees in one Department and the other way around. I have searched on Google but was unlucky.I am not sure that the way I am doing is right or wrong.
Any help would be appreciated. Please see the code. Thanks in advance
---------------------------Department--------------------------
public class Department
{
private int _DepartID;
private string _DepartName;
private string _DepartDescp;
public int DepartmentID
{
get { return _DepartID; }
set { _DepartID = value; }
}
public string DepartmentName
{
get { return _DepartName; }
set { _DepartName = value; }
}
public string DepartDescription
{
get { return _DepartDescp; }
set { _DepartDescp = value; }
}
public override string ToString()
{
return this.DepartmentID + " " + this._DepartName +" " + this.DepartDescription;
}
List<employee> EmployeeList=new List<employee>();
public List<employee> Empoyees(Employee objEmp)
{
EmployeeList.Add(objEmp);
return EmployeeList;
}
}
------------------------------------员工-------- ----------------
------------------------------------Employee------------------------
public class Employee
{
private int _employeeID;
private string _empFirstName;
private string _empLastName;
private string _empTitle;
public int EmployeeID
{
get { return _employeeID; }
set { _employeeID = value; }
}
public string EmpFirstName
{
get { return _empFirstName; }
set { _empFirstName = value; }
}
public string EmpLastName
{
get { return _empLastName; }
set { _empLastName = value; }
}
public string EmpTitle
{
get { return _empTitle; }
set { _empTitle = value; }
}
public override string ToString()
{
return this.EmpTitle + " " + this.EmpFirstName + " " + this._empLastName + " " + this._employeeID.ToString ();
}
}
-------------------按钮后面的代码-------------------
-------------------Code behind button-------------------
Department obj_Depart = new Department();
Employee obj_Emp = new Employee();
obj_Emp.EmployeeID =1011;
obj_Emp.EmpTitle = "Mr";
obj_Emp.EmpFirstName = "Test1";
obj_Emp.EmpLastName = "Test2";
obj_Depart.DepartmentID = 1201;
obj_Depart.DepartmentName = "IT Department";
obj_Depart.DepartDescription = "Business center";
obj_Depart.Empoyees(obj_Emp);
listBox1.Items.Add(obj_Depart);
推荐答案
var allEmployees = Departments.SelectMany(d => d.EmployeeList);
如果你想得到一个雇员的侮辱
if you want to get the deparment of an employee
var employeeDepartment = Departments.FirstOrDefault(d => d.Employees.FirstOrDefault(e => e == employee) != null));
但是,我认为您会发现,在Employee
类中具有Deparment
属性比在Department
类中具有集合要容易得多.
注意:使用id进行关联更像是一种关系数据概念,并且您正在使用C#进入对象世界.
您的问题还不清楚,所以我只是在猜测您想要的是什么.
However, I think you will find it easier to have a Deparment
property in the Employee
class than to have a collection in the Department
class.
Note: Using id''s for associations is more of a relational data concept, and you are in the object world with C#.
Your question is a bit unclear, so I am only guessing at what you want.
public class Department
{
public int DepartmentID { get; set; }
public string DepartmentName { get; set; }
public string DepartDescription { get; set; }
}
public class Employee
{
public int EmployeeID { get; set; }
public string EmpFirstName { get; set; }
public string EmpLastName { get; set; }
public string EmpTitle { get; set; }
public int DepartmentId { get; set; }
}
static void Main(string[] args)
{
//unlayered style
List<Department> departments = new List<Department>();
List<Employee> employees = new List<Employee>();
Department d = new Department();
d.DepartmentID = 1;
d.DepartmentName = "xyz";
d.DepartDescription = "abc";
departments.Add(d);
Department d1 = new Department();
d1.DepartmentID = 2;
d1.DepartmentName = "xyz2";
d1.DepartDescription = "abc2";
departments.Add(d1);
Employee e;
for (int i = 0; i < departments.Count; i++)
{
for (int j = 0; j < 4; j++)
{
e = new Employee();
e.DepartmentId = departments[i].DepartmentID;
e.EmpFirstName = "add";
e.EmpLastName = "test";
e.EmployeeID = 123;
e.EmpTitle = "adf";
employees.Add(e);
}
}
//querying for an employee name in a particular department
string employeename = employees.Where(x => x.DepartmentId == departments.Where(y => y.DepartmentName == "xyz").Select(y => y).FirstOrDefault().DepartmentID).Select(x => x.EmpFirstName).FirstOrDefault();
Console.WriteLine(employeename);
//layered style - using Business Logic class to define an employee list prop which you can add to
DepartmentBL dbl = new DepartmentBL();
for (int i = 0; i < departments.Count; i++)
{
for (int j = 0; j < 10; j++)
{
e = new Employee();
e.DepartmentId = departments[i].DepartmentID;
e.EmpFirstName = "add";
e.EmpLastName = "test";
e.EmployeeID = j+1;
e.EmpTitle ="adf" + (j+1).ToString();
dbl.EmployeeList.Add(e);
}
}
Console.WriteLine("Employees added :" + dbl.EmployeeList.Count.ToString());
}
Department和Employee数据类未定义任何操作,这些操作应该是单独的类(查找n层体系结构)的任务,在这种情况下为DepartmentBL(BL =业务层或逻辑).由于部门由"雇员组成,因此它公开了可以添加雇员"的雇员"列表属性.希望这在一定程度上有所帮助.
Department and Employee data classes don''t define any operation, that should be the task of a separate class (lookup n-tier architecture), in this case DepartmentBL (BL = Business Layer or Logic). It exposes an Employees list property that you can add Employees to, since a department "consists of" employees. Hope this helps to some extent.
这篇关于C#中的类关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!