title: Unable to cast the type 'System.Linq.IGrouping`2[[System.Decimal
body: <p>In my webApi2 service I am using Entity framework as my orm with Linq as query language backend as azure-sql.
  My DTO class to map my query results.</p>
  
  <pre><code>public class RTLSLevelZonesInfoDTO
  {
      public List&lt;int&gt; L1 { get; set; }
      public List&lt;int&gt; L2 { get; set; }
      public List&lt;int&gt; L3 { get; set; }
  
      public RTLSLevelZonesInfoDTO()
      {
  
          L1 = new List&lt;int&gt;();
  
          L2 = new List&lt;int&gt;();
          L3 = new List&lt;int&gt;();
  
      }
  }
  </code></pre>
  
  <p>My query to fill data.</p>
  
  <pre><code>var leveZones =  (from s in db.RTLS_SpotLevelMap
                          group s by s.Level_no
                          into g
                          select new RTLSLevelZonesInfoDTO
                          {
                              L1 = (g.Key == 1) ? (List&lt;int&gt;)g: convertedLocationArray,
                              L2 = (g.Key == 2) ? (List&lt;int&gt;)g : convertedLocationArray,
                              L3 = (g.Key == 3) ? (List&lt;int&gt;)g : convertedLocationArray
  
                          }).FirstOrDefault();
  </code></pre>
  
  <p>Above query fetches zoneids which belongs to particular level(only 3 fixed levels are there in my database as shown in below image)</p>
  
  <p><a href="https://i.stack.imgur.com/ztWPT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ztWPT.png" alt="enter image description here"></a></p>
  
  <p>After executing my code i was catch with an exception with message saying </p>
  
  <blockquote>
    <p>Unable to cast the type 'System.Linq.IGrouping2[[System.Decimal,
    mscorlib, Version=4.0.0.0, Culture=neutral,
    PublicKeyToken=b77a5c561934e089],[RTLSWebService.Models.RTLS_DB.RTLS_SpotLevelMap,
    RTLSWebService, Version=1.0.0.0, Culture=neutral,
    PublicKeyToken=null]]' to type
    'System.Collections.Generic.List`1[[System.Int32, mscorlib,
    Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'.
    LINQ to Entities only supports casting EDM primitive or enumeration
    types.</p>
  </blockquote>