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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:53:23+00:00 2026-05-26T23:53:23+00:00

I have an object with the following hierarchy class Account string username List Delegations

  • 0

I have an object with the following hierarchy

class Account
   string username
   List Delegations

class Delegation
   List SingleDelegations

class SingleDelegation
   string uid

Now I’d like to perform a query which loads an Account object with the according dependencies (i.e. Delegations and Delegations.SingleDelegations) already loaded (with that single query), but it should just load those matching a specified condition, namely

  • only those accounts where the username matches a parameter (easy)
  • only those SingleDelegation objects where the uid matches a given parameter

My Approach
I have the following method in my AccountRepository

public Account ReadAccountsByUsernameAndUid(string username, string uid)
{ 
    var matchingObjs = (from a in context.Accounts
                            from d in a.Delegations
                            from sd in d.SingleDelegations
                            where
                               a.username == username &&
                               sd.uid == uid
                            select new 
                            {
                               Account = a,
                               Delegation = d,
                               SingleDelegation = sd
                            });

     //knowing there should be just one account (ignore the missing null check for now)
    return matchingObjs.FirstOrDefault<Account>().Account;          
}

This obviously returns an anonymous type object having the different objects exposed as properties. Since the Account and Delegation and SingleDelegation are linked over corresponding FKs my Account object will have them properly loaded (as the context knows them).

Personally, this looks weird. I have to create a new anonymous type for instructing EF to include the subobjects in the query s.t. in the end I get my Account object properly loaded.

My Question:
Is there a better, nicer way?

  • 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-26T23:53:23+00:00Added an answer on May 26, 2026 at 11:53 pm

    No there is no better or nicer way. This is how EF works. If you want to filter relations and retrieve all data with single query you must always use projection.

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

Sidebar

Related Questions

I have the following class hierarchy: class A(object): def __init__(self, filename, x): self.x =
We have the following class hierarchy: public interface IManager { object GetObject(int); } public
Suppose you have the following object hierarchy: class Vehicle { public: virtual ~Vehicle() {}
If I have the following domain object: public class Customer { public virtual Guid
Suppose I have the following class hierarchy: class A { int foo; virtual ~A()
Suppose we have the following class hierarchy: class Base { ... }; class Derived1
In my application, I have the following class hierarchy: class Word { ... }
Assume I have the following class-hierarchy in C++: class AbstractBaseClass { public: // Note:
Have a look at the following code: class A(object): defaults = {'a': 1} def
Suppose I have the following hierarchy: class A { public: A() private: int aa;

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.