Having difficulty trying to come up with an effective linq lambda expression
<p>Stats:</p>

<ul>
<li>ASP.NET 4.5.2</li>
<li>MVC 5</li>
<li>Identity 2.2</li>
</ul>

<p>I am able to make the SQL statement, as I know SQL decently enough, but I am rather new to the whole LINQ Lambda thing.</p>

<p>I have also looked into the two tools most often cited for this job, but sorry -- Linqer is unable to run because the Microsoft tool it uses to create the SQL connection (the dbml file) refuses to install on my Win8.1 system, and LinqPad doesn’t provide an actual translation until you actually buy the product (which makes the “trial” fundamentally broken in the first place, IMHO: how can you possibly evaluate something that prevents you from conducting the action that you are evaluating?).</p>

<p>I am hoping that someone can take my SQL statement below and convert it into a proper LINQ Lambda expression that I can more effectively plug into my controller. This is also going to be a part of a multi-model display, such that multiple lambda expressions will be collated into a model that displays multiple outputs (very similar but slightly different outputs, one for users that shows only the user's dashboard, one for managers that also need the user's dashboard in addition to their managerial dashboard).</p>

<p>My expression is thus:</p>

<pre><code>SELECT 
      co.CompanyId
    , co.CompanyName
    , co.CompanyCity
    , co.NumberOfEmployees
    , co.ProspectingScore
    , po.ProvinceAbbr
    , cd.PDFResourceLibrary
    , cd.PresentationDone
    , cd.MOUDone
FROM Company AS co
LEFT JOIN Province AS po ON co.ProvinceId = po.ProvinceId
OUTER APPLY (SELECT TOP 1 MAX(CycleDate) AS CycleDate, PDFResourceLibrary, PresentationDone, MOUDone FROM Cycle AS cy (NOLOCK) WHERE cy.CompanyId = co.CompanyId GROUP BY PDFResourceLibrary, PresentationDone, MOUDone) AS cd
WHERE co.RegionId = '66BD50DD-B6CB-E511-8265-14FEB5FBEAE8'
ORDER BY co.ProspectingScore DESC
</code></pre>

<p>For simplicity's sake, the po.ProvinceAbbr and its associated left join <em>can</em> be dropped, the RegionID will be brought in via a variable pulled from a claim, and if need be you can use wildcards (*) instead of named fields for all tables except for the MAX requirement on the Outer Apply (there are more than one cycles per company, I just want to bring back the most recent cycle by date). What you see above is the absolute minimum number of items needed to be pulled to populate the front end.</p>

<p>I have created the appropriate Models in my model namespace:</p>

<pre><code>public class HomeViewModel {
  public IEnumerable&lt;DashboardUserData&gt; RegionalCompanies { get; set; }
  public IEnumerable&lt;Company&gt; AllOtherCompanies { get; set; }
}
public class DashboardUserData {
  public Guid CompanyId { get; set; }
  public string CompanyName { get; set; }
  public string CompanyCity { get; set; }
  public string ProvinceAbbr { get; set; }
  public int? NumberOfEmployees { get; set; }
  public int? ProspectingScore { get; set; }
  public bool? PDFResourceLibrary { get; set; }
  public bool? PresentationDone { get; set; }
  public bool? MOUDone { get; set; }
}
</code></pre>

<p>And I hope to attach the lambda expression to the RegionalCompanies as shown in the model.</p>

<p>As an example, the AllOtherCompanies (which is the Managerial dashboard) has as its original lambda expression the following:</p>

<pre><code>viewModel.AllOtherCompanies = await db.Company.Where(c =&gt; c.RegionId != regionId).Include(c =&gt; c.Province).ToListAsync();
</code></pre>

<p>So you can see that I am at least halfway there -- I am able to bring in the Company table, but I have no clue how to include the Cycle table for each company such that only the most recent Cycle is included with the company and all others are dropped.</p>

<p>Frankly, with the lambda I am still unsure as to how to connect the Cycle table such that only the most recent cycle (by date) is chosen. All other cycles for any company MUST be ignored and dropped, so <em>from what I can tell</em> neither a plain Join nor a GroupJoin would be effective here.</p>

<p>Suggestions?</p>