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

The Archive Base Latest Questions

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

What I am attempting to do is the following: I have a custom object

  • 0

What I am attempting to do is the following:

I have a custom object OTest and OSubTest:

public class OTest
{
   public int TestId {get; set;}
   public List<OSubTest> SubTests {get; set;}
}

public class OSubTest
{
   public int SubTestId {get; set;}
}

Now, I will run a LINQ to SQL query which will look like this:

var query = from t in Tests 
select new OTest
{
   TestId = t.TestId
};

The question is, how can I get the “Subtests” as part of the query and as a list property in my main OTest object without the LINQ running 100’s of sub queries off the main query for the data.

If I did it like this then it will execute a query for each record that is returned, which is not very practical:

var query = from t in Tests 
select new OTest
{
   TestId = t.TestId,
   SubTests = (from st in SubTests where st.TestId = t.TestId select new OSubTest{SubTestId = st.SubTestId}).ToList()
};

Any thoughts on how I can get my List property to fill with a single query? Perhaps with left joins?

Any help would be appreciated.

If you need any clarification please ask me.

EDIT

Okay, taking a look at this, it does work when I have 1 sub List, but as soon as I add a 2nd one the 2nd one queries the database 100’s of times. Here is my exact query:

public List<OFile> GetFiles(int fileGroupId)
        {
            var query = (from f in CoreContext.Files
                         join fgf in CoreContext.FileGroupFiles on f.FileId equals fgf.FileId
                         where fgf.FileGroupId == fileGroupId
                         select new OFile
                                    {
                                        ActiveFile = f.ActiveFile,
                                        FileExtension = f.FileType,
                                        FileGuid = f.FileIdentifier,
                                        FileName = f.FileName,
                                        FilePath = f.FilePath,
                                        FileSize = f.FileSize,
                                        FileTitle = f.FileTitle,
                                        RevisionId = f.RevisionId,
                                        ParentFileId = f.ParentFileId,
                                        FileId = f.FileId,
                                        Attributes = f.FileAttributeFiles.Where(id => id.FileId == f.FileId).Select(
                                            id => new OFileAttribute
                                                      {
                                                          FileAttributeId = id.FileAttributeId,
                                                          Value = id.Value
                                                      }).ToList(),
                                        Tags = f.FileTags.Where(id => id.FileId == f.FileId).Select(
                                            id => new OFileTag
                                                      {
                                                          FileTagId = id.FileTagId,
                                                          TagName = id.TagName,
                                                      }).ToList(),


                                    });



            return query.ToList();
        }

The Attributes will query the database 100’s of times and does not show up as a JOIN.

The FileAttributeFiles table has a FK to an Attributes table.

Thanks

  • 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-13T23:39:36+00:00Added an answer on May 13, 2026 at 11:39 pm

    If I did it like this then it will execute a query for each record that is returned, which is not very practical

    Um, really? Have you tried it? I’m not sure what Tests and SubTests are supposed to be, but generally speaking, LINQ to SQL executes queries of this form:

    using (var c = new MyDataContext())
    {
        var q = from t in c.Tests
                select new 
                       {
                           TestId = t.TestId,
                           SubTests = from st in t.SubTests 
                                      where st.TestId = t.TestId 
                                      select new 
                                      {
                                          SubTestId = st.SubTestId
                                      }
                        };
    }
    

    …in one query. Projecting onto POCOs instead of anonymous types works, too. I don’t know if using ToList() changes anything, because I don’t use that, personally; it “just works” without it.

    • 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 structure for custom UITableViewCells: NumericEntryCell - Contains method for
I have the following code (attempting to log a user in programatically): List<GrantedAuthority> authorities
I have following entity (non-relevant fields/methods are removed). public class HitsStatsTotalDO { @Id transient
I am attempting to setup a custom attribute like the following: [AttributeUsageAttribute(AttributeTargets.Method)] public sealed
I have the following class structure that I need to unit test: public interface
I am attempting to set up a custom downloader middleware class in Scrapy. I
Attempting a beginner's tutorial. I have the following in my head: <script type=text/javascript charset=utf-8
I have been attempting to learn Grails, however I am running into the following
I am attempting to run the following command in KornShell (ksh): set -A INDEXES
I get the following error when attempting to connect to a web service via

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.