title: How to Return Extra Data with IQueryable method?
body: <p>I am using Entity Framework and Breeze.  For an Entity, there is a bit of associated data I would like to provide with the entity.  Getting this data is most efficiently done by querying the Entity table and joining to other tables; this query includes a group by sub-query.  </p>
  
  <p>I am attempting to tack this extra data on by adding it as a [NotMapped] field to the entity:</p>
  
  <pre><code>[NotMapped]
  public string NotMappedField { get; set; }
  </code></pre>
  
  <p>So then I basically want to replace this webapi controller method</p>
  
  <pre><code>[HttpGet]
  public IQueryable&lt;MyObject&gt; MyObjects()
  {
      return _contextProvider.Context.MyObjects;
  }
  </code></pre>
  
  <p>With something like this:</p>
  
  <pre><code>public IQueryable&lt;MyObject&gt; MyObjectsWithExtraData()
  {
     var query = from o in _contextProvider.Context.MyObjects
                 // big complex query
                 select new MyObject
                 {
                     FieldA = o.FieldA,
                     FieldB = o.FieldB,
                     // all fields
                     NotMappedField = x.ResultFromComplexJoin
                 }
     return query;
  }
  </code></pre>
  
  <p>This gives me an error:</p>
  
  <blockquote>
    <p>The entity or complex type 'MyObject' cannot be constructed in a LINQ to Entities query.</p>
  </blockquote>
  
  <p>I've tried this a few ways and it seems to fight me both from the EF side and the Breeze side.  I need to keep this as returning something like IQueryable so I can filter from the client through webapi because doing something like a ToList() here causes memory issues due to the dataset size.  </p>
  
  <p>So my question is - is there a best practices kind of way to accomplish what I am attempting or can anyone provide a solution?</p>
  
  <p><strong>Update:</strong></p>
  
  <p>I have found you can return extra data alongside of your entity and still have access to the entity as a queryable from Breeze:</p>
  
  <pre><code>public object MyObjectsWithExtraData()
  {
      var query = from o in _contextProvider.Context.MyObjects
                  // big complex query....
                  select new
                  {
                      theObject = MyObject,
                      NotMappedField = x.ResultFromComplexJoin
                  };
      return query;
  }
  </code></pre>
  
  <p>and then from the client breeze side you can do something like this:</p>
  
  <pre><code>var query = breeze.EntityQuery
             .from("MyObjectsWithExtraData")
             .where("theObject.FieldA", "Equals", 1)
             .expand("theObject.SomeNavigationalProperty")
             .orderBy("theObject.FieldB");
  </code></pre>
  
  <p>Still not exactly what I was looking for but it is actually pretty slick.</p>