Note that there are some explanatory texts on larger screens.

plurals
  1. POLinq to get elements in a list (parent) where a condition is satisfied on property of an object in a child list?
    primarykey
    data
    text
    <p>I was wondering if someone could help me in generating a linq query for the following scenario.</p> <p>Here are the classes with the relevant properties:</p> <pre><code>public class Employee { IList&lt;Employee&gt; DirectReports { get; set;} IList&lt;BonusPlan&gt; BonusPlans { get; set;} BonusPlanTemplate BonusPlanTemplate { get; set;} } public class BonusPlan { FiscalPeriod FiscalPeriod { get; set; } Employee Employee { get; set;} } </code></pre> <p>I'm trying to create a method:</p> <pre><code>IEnumerable&lt;Employee&gt; GetDirectReportsWithoutBonusPlansCreatedForFiscalPeriod(FiscalPeriod fiscalPeriod) </code></pre> <p>So basically I have this to get the directreports with bonus plans for a particular fiscal period:</p> <pre><code>var query = from dr in DirectReports from bp in dr.BonusPlans where bp.Employee.BonusPlanTemplate != BonusPlanTemplate.Empty &amp;&amp; bp.FiscalPeriod==fiscalPeriod select dr; IList&lt;Employee&gt; directReportsWithBonusPlansCreated = query.ToList(); </code></pre> <p>Then I get all of the DirectReports that should have bonus plans setup (indicated by having a BonusPlanTemplate assigned) that aren't in the list from the previous query.</p> <pre><code>var query2 = from dr in DirectReports where dr.BonusPlanTemplate != BonusPlanTemplate.Empty &amp;&amp; !directReportsWithBonusPlansCreated.Contains(dr) select dr; </code></pre> <p>This produces the correct results but it seems like there must be another way. I'm not sure if I need to do this in two steps. Can someone please help me to combine these two linq queries and possibly make it more efficient. I have relatively little experience with Linq.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload