0% found this document useful (0 votes)
32 views

Generic Collections

Uploaded by

rupams2024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Generic Collections

Uploaded by

rupams2024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GenericCollection_Disctionary
{
class Program
{
static void Main(string[] args)
{
int departmentID; decimal salary;
Employee emp1 = new Employee()
{
EmployeeID = 1001,
EmployeeName = "Ram",
Salary = 15000,
DepartmentID = 1
};

Employee emp2 = new Employee()


{
EmployeeID = 1002,
EmployeeName = "Laxman",
Salary = 17000,
DepartmentID = 2
};

Employee emp3 = new Employee()


{
EmployeeID = 1003,
EmployeeName = "Bharat",
Salary = 15500,
DepartmentID = 1
};

Employee emp4 = new Employee()


{
EmployeeID = 1004,
EmployeeName = "Bheem",
Salary = 16500,
DepartmentID = 2
};

Employee emp5 = new Employee()


{
EmployeeID = 1005,
EmployeeName = "Arjun",
Salary = 18500,
DepartmentID = 1
};

List<Employee> listEmployee = new List<Employee>();


listEmployee.Add(emp1);
listEmployee.Add(emp2);
listEmployee.Add(emp3);
listEmployee.Add(emp4);
listEmployee.Add(emp5);
foreach (Employee emp in listEmployee)
{
Console.WriteLine($"EmployeeID:{emp.EmployeeID}\t, Name:
{emp.EmployeeName}\t, Salary:{emp.Salary}\t, DepartmentID:{emp.DepartmentID}");
}

Console.WriteLine("--------------------------------------------------");

Dictionary<int, decimal> dictionaryEmployees = new Dictionary<int,


decimal>();
for (int i = 0; i <= listEmployee.Count - 1; i++)
{
departmentID = listEmployee[i].DepartmentID;
salary = listEmployee[i].Salary;

for (int j = i + 1; j <= listEmployee.Count - 1; j++)


{
if (departmentID == listEmployee[j].DepartmentID)
salary += listEmployee[j].Salary;
}

if (!dictionaryEmployees.ContainsKey(departmentID))
dictionaryEmployees.Add(departmentID, salary);

}
Console.WriteLine("Department wise sum of salary:");
Console.WriteLine("----------------------");

foreach (int emp in dictionaryEmployees.Keys)


{
Console.WriteLine($"{emp}\t\t{dictionaryEmployees[emp]}");
}
Console.ReadKey();
}
}

public class Employee


{
public int EmployeeID { get; set; }
public string EmployeeName { get; set; }
public decimal Salary { get; set; }
public int DepartmentID { get; set; }
}
}

You might also like