Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6037561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:05:32+00:00 2026-05-23T06:05:32+00:00

I was wondering if someone could help me in generating a linq query for

  • 0

I was wondering if someone could help me in generating a linq query for the following scenario.

Here are the classes with the relevant properties:

public class Employee 
{
    IList<Employee> DirectReports { get; set;}
    IList<BonusPlan> BonusPlans { get; set;}

    BonusPlanTemplate BonusPlanTemplate { get; set;}
}

public class BonusPlan
{
    FiscalPeriod FiscalPeriod { get; set; }
    Employee Employee { get; set;}
}

I’m trying to create a method:

IEnumerable<Employee> GetDirectReportsWithoutBonusPlansCreatedForFiscalPeriod(FiscalPeriod fiscalPeriod)

So basically I have this to get the directreports with bonus plans for a particular fiscal period:

var query = from dr in DirectReports
            from bp in dr.BonusPlans
            where bp.Employee.BonusPlanTemplate != BonusPlanTemplate.Empty && 
              bp.FiscalPeriod==fiscalPeriod
            select dr;

IList<Employee> directReportsWithBonusPlansCreated = query.ToList();

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.

var query2 = from dr in DirectReports
             where dr.BonusPlanTemplate != BonusPlanTemplate.Empty &&
                !directReportsWithBonusPlansCreated.Contains(dr)
             select dr;

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T06:05:33+00:00Added an answer on May 23, 2026 at 6:05 am

    Do you need the first query for any other reason? If not, it’s pretty easy:

    var query = from dr in DirectReports
                where dr.BonusPlanTemplate != BonusPlanTemplate.Empty
                   && !dr.BonusPlans.Any(bp => bp.FiscalPeriod == fiscalPeriod)
                select dr;
    

    You could make your life easier use an extra method in Employee:

    public bool HasBonusPlanForPeriod(FiscalPeriod period)
    {
        return BonusPlans.Any(bp => bp.FiscalPeriod == fiscalPeriod);
    }
    

    Then your original first query becomes:

    var query = from dr in DirectReports
                where dr.BonusPlanTemplate != BonusPlanTemplate.Empty && 
                      dr.HasBonusPlanForPeriod(fiscalPeriod)
                select dr;
    
    IList<Employee> directReportsWithBonusPlansCreated = query.ToList();
    

    and the second query becomes:

    var query = from dr in DirectReports
                where dr.BonusPlanTemplate != BonusPlanTemplate.Empty && 
                      !dr.HasBonusPlanForPeriod(fiscalPeriod)
                select dr;
    
    IList<Employee> directReportsWithBonusPlansCreated = query.ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just wondering if someone could help me with the following issue. On my local
Was wondering if someone could help me out a little with this query: SELECT
I was wondering if someone could help me. Suppose I have some classes as
I was wondering if someone could help with the following problem. I have a
I was wondering if someone could help me to get this method converted to
I was wondering if someone could help with an example criteria for nhibernate. I
I am wondering if someone could help me with some probably rather simple code,
I was wondering if someone could help me get pointers to solve this problem.
I was wondering if someone could help me using the JQuery validation plugin. I
I was wondering if someone could help me understand this problem. I prepared a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.