title: Entityframework error: "The type '......' appears in two structurally incompatible initializations within a single LINQ to Entities query
body: <p>I am having trouble generating an IQuerable result from a group by clause in linq to Entities for range of values.</p>
  
  <pre><code>    IQueryable&lt;Model.MyEntity&gt; query = MyContext.GetDbSet()      
      IQueryable&lt;MyObject&gt; query2 = null;
          query2 = query.Select(x =&gt; new MyObject()
          {           
  
              GroupingColumn = SqlFunctions.StringConvert(arrayMin.AsQueryable().FirstOrDefault(s =&gt; x.Amount &gt; s)) + 
              "-" + SqlFunctions.StringConvert(arrayMax.AsQueryable().FirstOrDefault(s =&gt; x.Amount &lt; s)) ,
          CountOfAmountRange = 1,
               SumOfAmount = (decimal)x.Amount,
          });
          query2 = query2.GroupBy(cm =&gt; new {cm.GroupingColumn }).Select(y =&gt; new MyObject()
          {
              GroupingColumn =y.Key.GroupingColumn ,
              CountOfAmountRange = y.Count(),
              SumOfAmount = (decimal)y.Sum(p =&gt; p.SumOfAmount)
        });
  </code></pre>
  
  <p>A bit of context:
  I am working on a highly structured application which has a layer responsible for generating queries which will be later applied to retrieve data from context in another layer. I have successfully used this to generate many reports but this specific one throws the error below.</p>
  
  <blockquote>
    <p>The type 'MyObject' appears in two structurally incompatible
        initializations within a single LINQ to Entities query. A type can be
        initialized in two places in the same query, but only if the same
        properties are set in both places and those properties are set in the same order.</p>
  </blockquote>
  
  <p>The arrayMax and arrayMin are arrays of decimal values containing the maximum and minimum values respectively for comparison and generating of string values for the GroupingColumn. </p>
  
  <p>I have seen a couple of questions relating this error on StackOverflow but none of the answers seem to show me the solution to my problem.</p>