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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:08:51+00:00 2026-05-25T23:08:51+00:00

I have a database with two entities, A and B. A and B have

  • 0

I have a database with two entities, A and B. A and B have a many-to-many relationship between them (there’s also an AB table too that is created automatically to realize this). A has A_prop (key) and B has B_prop (key).

I want to, given a specific B_prop, find all the A’s that have in its relationship of B’s, (call them Ayes and Bees for the respective Navigation Properties), the B with the specific B_prop.

So I have this code:

public class Repository
{
    private ABEntities entities = new ABEntities();

    public IQueryable<A> FindAllA(string b_prop)
    {
        return from b in entities.Bs
        where b.B_prop == b_prop
        select b.Ayes;
    }
}

The return types here doesn’t match. What I would really like is to have a List of A’s, or something similar that I can work with as in the following manner:

List<A> listofa = repository.FindAllA("some string");
foreach (A a in listofa)
{
    // Do my stuff here.
}

EDIT:

Thanks for the replies. This is a solution (tested) to my problem:

public List<A> FindAllA(string b_prop)
{
    return (from b in entities.Bs
            where b.B_prop == b_prop
            select b.Ayes).First().ToList();
}
  • 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-25T23:08:52+00:00Added an answer on May 25, 2026 at 11:08 pm

    Rather than calling .First on your initial subset, consider using SelectMany to flatten your result set:

    public List<A> FindAllA(string b_prop) 
    { 
        return (from b in entities.Bs 
                where b.B_prop == b_prop
                from a in b.Ayes
                select a).ToList(); 
    } 
    

    This way if for some reason you had multiple records that matched in your Bs table, all of the associated As would be returned rather than just the first set matching up to your first B result. The problem with your first try is that you were returning IQueryable<EntitySet<Ayes>> rather than IQueryable<Ayes>. SelectMany flattens this relationship out.

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

Sidebar

Related Questions

I have a many to many relationship between entities and there is a table
I have two entities with many-to-many relationship defined on them. <set name=TreasuryCodes table=ServiceProviderAccountTreasuryCode lazy=true
I have two classes that represent two different database entities. Their relationship is 1:m
I have one many-to-many relationship in my model. There are two entities, Emergencies and
I have two entities that have a many-to-many relationship; they are mapped with annotations
I have a has_many relationship between two entities, Feeds and Posts. I also have
I have a many-to-many relationship between two entities - Media and MediaCollection. I want
I have a question about One-To-Many relationship between two tables when there is the
I have two entities Foo and Bar with a Many to Many relationship between
Consider the following scenario We have a simple database that involves two entities: user

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.