- In C# collection types are designed to store, manage and manipulate similar data more efficiently. Data manipulation includes adding, removing, finding, and inserting data in the collection. we will create once console application and see the Dictionary Generic Collections
- First we have to create one object for employee
-
public class Employee { [Key] public int ID { get; set; } [Required] [Display(Name="Please Enter Name")] public string Name { get; set; } public string Email { get; set; } public int Age { get; set; } }
- and also we have added the List object
public class EmployeeViewModel { public List<Employee> employees { get; set; } }
- Then we create one controller name as home controller and add Interface
-
private Iemployee _iemp; public HomeController(Iemployee iemployee) { _iemp = iemployee; } public ActionResult Index() { return View(_iemp.emp()); }
public interface Iemployee { EmployeeViewModel emp(); }
- Now we have to add Repository for our business logic. in this Repository we have added class object and you can see we have bind data from Dictionary and convert into list object
-
public class EmployeeRepository:Iemployee { public EmployeeViewModel emp() { EmployeeViewModel model = new EmployeeViewModel(); Employee em = new Employee(); List<Employee> lemp = new List<Employee>(); Dictionary<int, Employee> dct = new Dictionary<int, Employee>(); dct.Add(1, new Employee { Name="test",Email="test@gmail.com",Age=10 }); dct.Add(2, new Employee { Name = "xyz", Email = "xyz@gmail.com", Age = 12 }); dct.Add(3, new Employee { Name = "abc", Email = "abc@gmail.com", Age = 15 }); for (var i = 1; i < dct.Count ; i++) { lemp.Add(dct[i]); } model.employees = lemp; return model; } }