public IEnumerable <CustomerViewModel> GetCustomers() { var customers = db.Customers.ToList(); var customersViewModels = new List <CustomerViewModel>(); foreach (var customer in customers) { var customerViewModel = new CustomerViewModel() { ID = customer.Customer_Id_Pk, Name = customer.Customer_Name, Details = customer.Customer_Details, Address = customer.Customer_Address, Contact = customer.Customer_Contact, IsActive = customer.Is_Active, CreatedBy = customer.Created_By, CreatedOn = customer.Created_On, ModifiedBy = customer.Modified_By, ModifiedOn = customer.Modified_On }; var customerNumberViewModels = new List <CustomerNumberViewModel>(); var CustomerNumbers = db.CustomerNumbers.Where(c => c.Customer_Id_Fk == customer.Customer_Id_Pk).ToList(); foreach (var CustomerNumber in CustomerNumbers) { var customerNumberViewModel = new CustomerNumberViewModel() { ID = CustomerNumber.Customer_Number_Id_Pk, NumberValue = CustomerNumber.Customer_Number_Value, NumberDetail = CustomerNumber.Customer_Number_Details }; customerNumberViewModels.Add(customerNumberViewModel); } customerViewModel.CustomerNumbers = customerNumberViewModels; customersViewModels.Add(customerViewModel); } return(customersViewModels); }
public IHttpActionResult GetCustomer(int id) { var customer = db.Customers.Find(id); if (customer == null) { return(NotFound()); } var customerViewModel = new CustomerViewModel() { ID = customer.Customer_Id_Pk, Name = customer.Customer_Name, Details = customer.Customer_Details, Address = customer.Customer_Address, Contact = customer.Customer_Contact, IsActive = customer.Is_Active, CreatedBy = customer.Created_By, CreatedOn = customer.Created_On, ModifiedBy = customer.Modified_By, ModifiedOn = customer.Modified_On }; var customerNumberViewModels = new List <CustomerNumberViewModel>(); var CustomerNumbers = db.CustomerNumbers.Where(c => c.Customer_Id_Fk == customer.Customer_Id_Pk).ToList(); foreach (var CustomerNumber in CustomerNumbers) { var customerNumberViewModel = new CustomerNumberViewModel() { ID = CustomerNumber.Customer_Number_Id_Pk, NumberValue = CustomerNumber.Customer_Number_Value, NumberDetail = CustomerNumber.Customer_Number_Details }; customerNumberViewModels.Add(customerNumberViewModel); } customerViewModel.CustomerNumbers = customerNumberViewModels; return(Ok(customerViewModel)); }