title: How to fix Entity Framework Core "Argument types do not match" with Linq Select projection
body: <p>When attempting a straight forward projection using Entity Framework Core and Linq, I am getting an "Argument types do not match" exception.</p>
  
  <p>I have looked into possible causes and have narrowed it down to the <code>Select</code> that is causing the error (see below). There is a <a href="https://github.com/aspnet/EntityFrameworkCore/issues/5522" rel="nofollow noreferrer">GitHub issue</a> describing a similar situation with simple types and optional navigation entities, but none of the suggested solutions have worked for me. It is not a <code>nullable</code> type and I have tried casting or using <code>Value</code> on any child properties. I have also tried setting the relationship to required in the <code>DbContext</code> which isn't exactly ideal.</p>
  
  <p>Here is the Linq query in the repository:</p>
  
  <pre><code>return await _dashboardContext.PresetDashboardConfig
      .Where(config =&gt; config.DashboardTypeId == dashboardType &amp;&amp; config.OrganisationType = organisationType)
      .GroupBy(config =&gt; config.GroupId)
      .Select(config =&gt; new DashboardConfigDTO
      {
          DashboardType = config.First().DashboardTypeId,
          OrganisationId = organisationId,
          WidgetGroups = config.Select(group =&gt; new WidgetGroupDTO
          {
              Id = group.Id,
              Name = group.GroupName,
              TabOrder = group.TabOrder,
  
              // Problem Select below:
              Widgets = group.Widgets.Select(widget =&gt; new WidgetConfigDTO
              {
                  IndicatorId = widget.IndicatorId,
                  ScopeId = widget.ScopeId.ToString(),
                  ParentScopeId = widget.ParentScopeId.ToString(),
                  WidgetType = widget.WidgetType,
                  WidgetSize = widget.WidgetSize,
                  Order = widget.Order
              })
          })
      })
      .SingleOrDefaultAsync();
  </code></pre>
  
  <p>And the entities:</p>
  
  <pre><code>public class DashboardConfig
  {
      public int Id { get; set; }
      public int DashboardTypeId { get; set; }
      public int OrganisationType {get; set; }
      public int GroupId { get; set; }
      public string GroupName { get; set; }
      public int TabOrder { get; set; }
  }
  
  public class PresetDashboardConfig : DashboardConfig
  {
      public ICollection&lt;PresetWidgetConfig&gt; Widgets { get; set; }
  }
  
  public class WidgetConfig
  {
      public int Id { get; set; }
      public int IndicatorId { get; set; }
      public long ScopeId { get; set; }
      public long? ParentScopeId { get; set; }
      public int WidgetType { get; set; }
      public int WidgetSize { get; set; }
      public int Order { get; set; }
  }
  
  public class PresetWidgetConfig : WidgetConfig
  {
      public int PresetDashboardConfigId { get; set; }
  }
  </code></pre>
  
  <p>And finally, the DbContext ModelBuilder:</p>
  
  <pre><code>modelBuilder.Entity&lt;PresetDashboardConfig&gt;(entity =&gt;
  {
      entity.Property(e =&gt; e.GroupName)
          .HasMaxLength(32)
          .IsUnicode(false);
  
      entity.HasMany(e =&gt; e.Widgets)
          .WithOne();
  });
  </code></pre>
  
  <p>Below are the DTO classes as per Henk's comment:</p>
  
  <pre><code>public class DashboardConfigDTO
  {
      public int DashboardType { get; set; }
      public int OrganisationId { get; set; }
      public IEnumerable&lt;WidgetGroupDTO&gt; WidgetGroups { get; set; }
  }
  
  public class WidgetGroupDTO
  {
      public int Id { get; set; }
      public string Name { get; set; }
      public int TabOrder { get; set; }
      public IEnumerable&lt;WidgetConfigDTO&gt; Widgets { get; set; }
  }
  
  public class WidgetConfigDTO
  {
      public int IndicatorId { get; set; }
      public string ScopeId { get; set; }
      public string ParentScopeId { get; set; }
      public int WidgetType { get; set; }
      public int WidgetSize { get; set; }
      public int Order { get; set; }
  }
  </code></pre>