title: How do I join 2 tables to create a new instance of an object using linq/lambda expressions in Entity Framework Core in C#?
body: <p>I'm using Entity Framework Core with PostGreSQL in an ASP.NET Core Web API.</p>
  <p>I'm having difficulty figuring out the appropriate lambda/linq expression for
  populating a field from a query.</p>
  <p>I could do it long hand with a foreach() loop, but I'm sure there's a more efficient way to complete the task.</p>
  <p>Consider the following table definitions:</p>
  <pre><code>public class A
  {
      [Key]
      public Guid AId { get; set; }
      public string AName { get; set; }
      public Guid BId { get; set; }
      [NotMapped] 
      public B BType { get; set; }
  }
  
  public class Consumer
  {
      [Key]
      public Guid BId { get; set; }
      public string BName { get; set; }
      public string OtherInfo { get; set; }
  }
  
  public DbSet&lt;A&gt; As { get; set; }
  public DbSet&lt;B&gt; Bs { get; set; }
  
  public A GetAById(Guid p_Id)
  {
      A result = null;
  
      using (Scope scope = AsyncScopedLifestyle.BeginScope(_container))
      {
          var dbContext = _container.GetInstance&lt;FooDBContext&gt;();
          result = dbContext.As.Where(x =&gt; x.AId == p_Id).FirstOrDefault();
          // How would I populate A-&gt;B with corresponding value from Bs Table?
      }
  
      return result;
  }
  
  result = dbContext.As.Where(x =&gt; x.AId == p_Id).FirstOrDefault();
  </code></pre>
  <p>How would I populate <code>A -&gt; B</code> with corresponding value from Bs table?</p>
  <p>I assume that I would do some sort of select and project results into a new instance of <strong>A</strong>?</p>