问题描述
我是.Net的新手。我们有一个任务构建到类Department,Employee。在Department中,有两个属性DeptId,DeptName。在Employee中,有四个属性EmpId,EmpName,EmpSalary,Department of object。因此,部门类值将自动绑定到员工类。我们使用集合(即arraylist)填充值。我们需要借助字典获取相同部门类型的员工详细信息。
我们这样试过。
使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;
使用 System.Collections;
命名空间员工
{
public class 部门
{
public int deptId { get ; set ; }
public string deptName { get 跨度>; set ; }
}
public class 员工
{
private employee.Department dept;
public 员工(employee.Department dept)
{
/ / TODO:完成成员初始化
this .dept = dept;
}
public int empId {获得跨度>; set ; }
public string empName { get 跨度>; set ; }
public int empSalary { get 跨度>; set ; }
public 部门部门{ get ; set ; }
}
class 计划
{
静态 void Main( string [] args)
{
ArrayList list = new ArrayList();
Department dept = new Department();
dept.deptId = 1 ;
dept.deptName = IT;
list.Add(dept);
ArrayList list1 = new ArrayList();
员工emp = 新员工(部门);
emp.empId = 101 ;
emp.empName = anusha;
emp.empSalary = 9000 ;
emp.Department = dept;
list1.Add(emp);
emp = new 员工(部门);
emp.empId = 102 ;
emp.empName = Sindhu;
emp.empSalary = 9000 ;
emp.Department = dept;
list1.Add(emp);
dept = new 部门();
dept.deptId = 2 ;
dept.deptName = HR;
list.Add(dept);
emp = new 员工(部门);
emp.empId = 103 ;
emp.empName = usha;
emp.empSalary = 2000 ;
emp.Department = dept;
list1.Add(emp);
emp = new 员工(部门);
emp.empId = 104 ;
emp.empName = indhu;
emp.empSalary = 3000 ;
emp.Department = dept;
list1.Add(emp);
var query = 来自员工员工 list1
其中 employee.empId == 101
选择员工;
Console.WriteLine( EmpID\tEmpName\tEmpSal\tDepartmentID);
foreach (员工e 查询)
{
控制台。 WriteLine(e.empId + \t + e.empName + \t + e.empSalary + \t + e.Department.deptId + ) ;
}
var query1 =(来自员工employee1 list1
join 部门department1 list1.Department.deptId上的
等于department1.deptId 进入 o
来自 department1 in o.DefaultIfEmpty()
select employee1);
Console.WriteLine( ---- **** ---- );
Console.WriteLine( EmpID\tEmpName\tEmpSal\tDepartmentName);
foreach (员工e1 in query1)
{
Console。 WriteLine(e1.empId + \t + e1.empName + \t + e1.empSalary + \t + e1.Department.deptName + 跨度>);
}
var query2 =(来自员工employee2 in list1
其中 employee2.empSalary > 1000 && employee2.empSalary< = 10000
选择 employee2);
Console.WriteLine( ---- **** ---- );
Console.WriteLine( EmpID\tEmpName\tEmpSal\tDepartmentID);
foreach (员工e2 in query2)
{
Console。 WriteLine(e2.empId + \t + e2.empName + \t + e2.empSalary + \t + e2.Department.deptId + 跨度>);
}
Console.ReadLine();
}
}
请通过字典获取有关获取emp值的帮助。
任意输入
-thanks&问候
Sindhu
Hi, I am new to .Net. We have a task to build to classes Department, Employee. In Department, two properties DeptId,DeptName. In Employee, four properties EmpId,EmpName,EmpSalary , object of Department. So that department class values will get automatically bounded to employee class. We populated values using collections(i.e arraylist). We need to get employee details of same department type with help of dictionary.
We tried like this.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace employee { public class Department { public int deptId { get; set; } public string deptName { get; set; } } public class Employee { private employee.Department dept; public Employee(employee.Department dept) { // TODO: Complete member initialization this.dept = dept; } public int empId { get; set; } public string empName { get; set; } public int empSalary { get; set; } public Department Department { get; set; } } class Program { static void Main(string[] args) { ArrayList list = new ArrayList(); Department dept = new Department(); dept.deptId = 1; dept.deptName = "IT"; list.Add(dept); ArrayList list1 = new ArrayList(); Employee emp = new Employee(dept); emp.empId = 101; emp.empName = "anusha"; emp.empSalary = 9000; emp.Department = dept; list1.Add(emp); emp = new Employee(dept); emp.empId = 102; emp.empName = "Sindhu"; emp.empSalary = 9000; emp.Department = dept; list1.Add(emp); dept = new Department(); dept.deptId = 2; dept.deptName = "HR"; list.Add(dept); emp = new Employee(dept); emp.empId = 103; emp.empName = "usha"; emp.empSalary = 2000; emp.Department = dept; list1.Add(emp); emp = new Employee(dept); emp.empId = 104; emp.empName = "indhu"; emp.empSalary = 3000; emp.Department = dept; list1.Add(emp); var query = from Employee employee in list1 where employee.empId == 101 select employee; Console.WriteLine("EmpID\tEmpName\tEmpSal\tDepartmentID"); foreach(Employee e in query) { Console.WriteLine(e.empId+"\t"+e.empName +"\t"+e.empSalary+"\t"+e.Department.deptId+""); } var query1 = (from Employee employee1 in list1 join Department department1 in list on employee1.Department.deptId equals department1.deptId into o from department1 in o.DefaultIfEmpty() select employee1); Console.WriteLine("----****----"); Console.WriteLine("EmpID\tEmpName\tEmpSal\tDepartmentName"); foreach (Employee e1 in query1) { Console.WriteLine(e1.empId + "\t" + e1.empName + "\t" + e1.empSalary + "\t" + e1.Department.deptName + ""); } var query2=(from Employee employee2 in list1 where employee2.empSalary > 1000 && employee2.empSalary<= 10000 select employee2); Console.WriteLine("----****----"); Console.WriteLine("EmpID\tEmpName\tEmpSal\tDepartmentID"); foreach (Employee e2 in query2) { Console.WriteLine(e2.empId + "\t" + e2.empName + "\t" + e2.empSalary + "\t" + e2.Department.deptId + ""); } Console.ReadLine(); } }
Please help regarding getting emp values by dept through dictionary.
Any Input
-thanks & regards
Sindhu
这篇关于在类中使用Dictionary过滤值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!