title: CRM 2011: Limitation of query expression?
body: <p>I believe the answer to this question may be to use Linq to Sql, but wanted to see if this is something which is possible using QueryExpressions:-</p>
  
  <p>I create a query expression which queries against Entity A, it also links to Entity B (via LinkEntity) and imposes additional criteria. It is possible to retrieve columns from Entity B by adding the appropriate attribute names. However, it will only retrieve the linked entity (inner join).</p>
  
  <p>Is it possible using QueryExpression to retrieve all related records (and required columns) from Entity B related to Entity A (e.g. all cases associated with contact where contact passes specified criteria). Normally I would consider inverting the query and searching for Entity B relatig to Entity A with the appropriate LinkEntity Conditions, but there are a number of linked entities which I would like to retrieve for the same contact query.</p>
  
  <p>So I'm left with some options:-</p>
  
  <p>(1) Perform a second query (not ideal when iterating over a large number of results from the initial query),
  (2) Perform a query using Linq to CRM on the filtered views,
  (3) A different method entirely?</p>
  
  <p>Any thoughts would be appreciated.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I ended up using Linq-to-Sql to complete this task and the code used is similar to that below (albeit with a few more joins for the actual query!):-</p>
  
  <pre><code>var dataCollection = (from eA in xrmServiceContext.EntityASet
  
  join eB in xrmServiceContext.EntityBSet on new  EntityReference(EntityA.EntityLogicalName, eA.Id) equals (EntityReference)eB.EntityBLookupToEntityA
  select new
  {
      Id = eA.Id,
      EntityBInterestingAttribute = eB.InterestingAttributeName 
  }
  </code></pre>
  
  <p>So this will bring back a row per Entity A, per Entity B. To make things easier I then defined a custom class "MyEntityAClass" which had properties which were Lists so I could return one object for filling of GridView etc. This is more to do with the processing of these results though so I haven't posted that code here.</p>
  
  <p>I hope that makes sense. Essentially, it is getting the multiple rows per record a la SQL which makes this method work.</p>