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

  • SEARCH
  • Home
  • 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 496087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:39:11+00:00 2026-05-13T05:39:11+00:00

I have a first query that returns a set of entities: var resultSet =

  • 0

I have a first query that returns a set of entities:

var resultSet = ....query....ToList();

which would return A, B, C, D, E

The entities inside this set are organized into chains because they have a reference (prevEntityId) pointing to the same type of entity, i.e.:

A -> B -> D

C -> E

I would like to write a second query so that only A and C are now returned but I have no idea how to write it.

I would prefer your answer with linq methods (like .Where()) instead of the new linq syntax.

Thank you

Update: sorry I initially used the wrong vocabulary in my question with the term “foreign key”. Actually an entity has a direct reference to the previous entity so to elect an entity in the second query, there must be no other entity that references it. I thought it would take 2 queries but if you think it can be done in one…

  • 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-13T05:39:11+00:00Added an answer on May 13, 2026 at 5:39 am

    EDIT: Okay, as the relationship is the opposite of what I expected, you’re looking for entities such that no entity has it as the previous one.

    One option:

     Where(x => !fullList.Any(y => y.PrevEntityId == x.Id));
    

    Now, if your query is actually being executed in SQL it would be worth seeing what that generates – it may well not be efficient. It pretty definitely won’t be efficient in LINQ to Objects. For LINQ to Objects, you’d be better off with:

     HashSet<string> prevIds = new HashSet<string>(fullList.Select(x => x.PrevId));
     var heads = fullList.Where(x => !prevIds.Contains(x));
    

    If you could more information about your environment, that would help us to help you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query which returns a set of numbers: SELECT Sequence FROM Table1
I have a query that works: DECLARE @ProductID int SET @ProductID = '1234' SELECT
I have this project set up with EF4 and I'm using LINQ to Entities
I have a dynamic query that returns around 590,000 records. It runs successfully the
The first thing i would like to say is that this is not exactly
I have two queries. The first query looks like this : SELECT subject_id, period,
I have a PHP/MySQL query that returns to an HTML table, and I'm stuck
I have a performance problem on a query. First table is a Customer table
I have First/Last/Previous/Next buttons that change the selected child node of a TreeViewItem. Setting
I have a model that holds user address. This model has to have first_name

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.