title: Linq Method To Get Data From Another Table Based On ID
body: <p>I have a customer table, where I have the basic information <em>name</em>, <em>address</em>, <em>postcode</em> etc. A company can have multiple customers. So the relationship is loosely related to the Company table with the foreign key <strong>CompanyId</strong>. To search for customers I use, a IQueryable method as,</p>
  
  <pre><code>private IQueryable&lt;CustomerModel&gt; MapCustomer(IQueryable&lt;Customer&gt; 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
              });
  }
  </code></pre>
  
  <p>As you can see, if I needed to search all I do is, call the function to return an <strong>IEnumerable</strong>, then use <strong>Where</strong> on it. </p>
  
  <pre><code>return MapCustomer(db.Customers.AsNoTracking().Where(x =&gt; x.Id == id)).FirstOrDefault();
  </code></pre>
  
  <p>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 <strong><em>Name</em></strong> of the company. What should I do to get this info? Any help is appreciated. </p>