I have a customer table, where I have the basic information name, address, postcode etc. A company can have multiple customers. So the relationship is loosely related to the Company table with the foreign key CompanyId. To search for customers I use, a IQueryable method as,
private IQueryable<CustomerModel> MapCustomer(IQueryable<Customer> query)
return (from a in query
select new CustomerModel
CompanyId = a.CompanyId,
CustomerId = a.Id,
CustomerName = a.Name,
AddressLine1 = a.AddressLine1,
PhoneNo = a.TelephoneNumber,
Postcode = a.PostCode,
ServiceCompletion = a.ServiceCompleteted,
ServiceDate = a.ServiceDate
As you can see, if I needed to search all I do is, call the function to return an IEnumerable, then use Where on it.
return MapCustomer(db.Customers.AsNoTracking().Where(x => x.Id == id)).FirstOrDefault();
The problem is I need to be able to populate the company name instead of the CompanyID. So when I get the result I would be able to see the Name of the company. What should I do to get this info? Any help is appreciated.