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 8430459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:35:54+00:00 2026-06-10T05:35:54+00:00

I am getting to grips with LINQ and I’m stuck on the basic concepts

  • 0

I am getting to grips with LINQ and I’m stuck on the basic concepts and techniques of implementing a lookup table into objects and then querying that object (or it’s association). This may be a dumb question and probably has an answer that I should have worked out myself. But I have yet to find an explanation that has made the method stick.

So I have created an example DB structure like so

Example Database

and I would like to two LINQ queries the first that gives me all of the example2 records that are related via the lookup to a specified object1 and the second that gives me all the example1 records that are related to a specified object2

hope someone out there can kick start my brain.

something like

var examples = (from e in db.examples where e.example2.id == id).ToList();

SQL Query written quickly

SELECT * FROM [dbo].[example1] one
JOIN [dbo].[examplelookup] lu ON one.[id] = lu.[example1id]
JOIN [dbo].[example2] two ON lu.[example2id] = two.[id]
WHERE one.[id] = 1

Just nipped off and created this which I think should explain a bit more

model

  • 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-06-10T05:35:56+00:00Added an answer on June 10, 2026 at 5:35 am

    Assuming

    example1-> examplelookup relation name is examplelookups

    example2-> examplelookup relation name is examplelookups

    examplelookup -> example1 relation name is example1

    examplelookup -> example2 relation name is example2

    given id of example1

    db.example1.Where(x=>x.id == id).SelectMany(x=>x.examplelookups).Select(x=>x.example2);
    

    and visa versa

    db.example2.Where(x=>x.id == id).SelectMany(x=>x.examplelookups).Select(x=>x.example1);
    

    EDIT: Additional update based on Gert Arnold’s suggested answer to use the any clause (this is after already accepted as an answer)

    db.example1.Where(x=>x.examplelookups.Any(y=>y.example2.id == id));
    

    and visa versa

    db.example2.Where(x=>x.examplelookups.Any(y=>y.example1.id == id));
    

    2nd Edit (again after already accepted and answer but after the question was modded to include the data model)

    db.Example1.Where(x=>x.Id == id).SelectMany(x=>x.Example2); // could have duplicates
    db.Example2.Where(x=>x.Example1.Any(y=>y.Id == id)); //alternate any form removing the duplicates
    

    and visa versa

    db.Example2.Where(x=>x.Id == id).SelectMany(x=>x.Example1); // could have duplicates
    db.Example1.Where(x=>x.Example2.Any(y=>y.Id == id)); //alternate any form removing the duplicates
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently getting to grips with mod_rewrite and have ran into a stumbling block
I seem to be having trouble getting to grips with implementing a simple JTable
I'm just getting to grips with ParseKit, read the Basic Grammar Syntax but it's
I'm getting to grips with RPC in Windows and I've got a basic client/server
I'm getting to grips with a project that uses Linq to SQL. I would
Basic D3.js question, getting to grips with the syntax! I'm using D3 and I
I'm just getting to grips with OOP PHP and while I have understood it
I've come from a java background, so i'm still getting to grips with some
Getting to grips with Visual Studio 2010, This compiles: var x = System.Web.Security.Membership.GetUser(); And
I'm just getting to grips with GUI programming in java. Here is a trivial

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.