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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:56:47+00:00 2026-05-31T11:56:47+00:00

I have a complex LINQ query like var results = from obj1 in context.ProcessBases

  • 0

I have a complex LINQ query like

        var results = from obj1 in context.ProcessBases
                      join obj2 in context.InspectorArticles
                      on obj1.ID equals obj2.ProcessBaseID
                      join obj3 in context.InspectorSamples
                      on obj2.ID equals obj3.InspectorArticleID
                      where obj1.ID == _processBaseID
                      select new {obj1, obj2, obj3,};

Now this result set will have only ONE ProcessBases, each ProcessBase will have MULTIPLE InspectorArticles and each InspectorArticle will have MULTIPLE InspectorSamples. So when I loop through the result set, I want to loop through each InspectorArticle, and then loop through each InspectorSample that belongs to that InspectorArticle, something like:

        foreach (InspectorArticle _iart in results.First().obj2)
        {
          ...          
            foreach (InspectorSample _isamp in results.First().obj3)
            {
               ...
            }

        }

But since I’ve called a .First() on my result set obviously I get this exception:

foreach statement cannot operate on variables of type x because x does
not contain a public definition for ‘GetEnumerator’

So how can I loop through each instance of InspectorArticle, and then loop through the number of InspectorSamples for that article?

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-31T11:56:49+00:00Added an answer on May 31, 2026 at 11:56 am

    Since you are joining the records together, this statement is not correct:

    Now this result set will have only ONE ProcessBases, each ProcessBase
    will have MULTIPLE InspectorArticles and each InspectorArticle will
    have MULTIPLE InspectorSamples.

    What you will actually have after you execute your query is an IEnumerable where each object in the IEnumerable contains a reference to a ProcessBase, an InspectorArticle and an InspectorSample. For example, using the code below in LinqPad will yield an IEnumerable with the following contents:

    Code:

    void Main()
    {
        var processBases = new List<ProcessBase>();
        var inspectorArticles = new List<InspectorArticle>();
        var inspectorSamples = new List<InspectorSample>();
        processBases.Add(new ProcessBase { ID = 1 });
        processBases.Add(new ProcessBase { ID = 2 });
        inspectorArticles.Add(new InspectorArticle { ID = 3, ProcessBaseID = 1 });
        inspectorArticles.Add(new InspectorArticle { ID = 4, ProcessBaseID = 1 });
        inspectorArticles.Add(new InspectorArticle { ID = 5, ProcessBaseID = 2 });
        inspectorSamples.Add(new InspectorSample { ID = 6, InspectorArticleID = 3 });
        inspectorSamples.Add(new InspectorSample { ID = 7, InspectorArticleID = 3 });
        inspectorSamples.Add(new InspectorSample { ID = 8, InspectorArticleID = 3 });
        inspectorSamples.Add(new InspectorSample { ID = 9, InspectorArticleID = 4 });
        inspectorSamples.Add(new InspectorSample { ID = 10, InspectorArticleID = 5 });
        inspectorSamples.Add(new InspectorSample { ID = 11, InspectorArticleID = 5 });
    
        var processBaseID = 1;
        var results =   from obj1 in processBases
                        join obj2 in inspectorArticles on obj1.ID equals obj2.ProcessBaseID
                        join obj3 in inspectorSamples on obj2.ID equals obj3.InspectorArticleID
                        where obj1.ID == processBaseID
                        select new { obj1, obj2, obj3 };
        Console.WriteLine(results);
    }
    
    public class ProcessBase
    {
        public int ID { get; set; }
    }
    
    public class InspectorArticle
    {
        public int ID { get; set; }
        public int ProcessBaseID { get; set; }
    }
    
    public class InspectorSample
    {
        public int ID { get; set; }
        public int InspectorArticleID { get; set; }
    }
    

    Results:

    linqpad results

    So, if you want to keep the Linq statement as is and loop through it with multiple foreach statements, you’ll need to use something like the code below:

    foreach(var article in results.GroupBy(g => g.obj2.ID))
    {
        Console.WriteLine("Article ID: #{0}", article.Key);
        foreach(var sample in results
                              .Where(s => s.obj3.InspectorArticleID == article.Key)
                              .Select(s => s.obj3))
        {
            Console.WriteLine("\tSample ID: #{0}", sample.ID);
        }
    }
    

    Using this code (and continuing the example from above) should get you the output:

    Article ID: #3
      Sample ID: #6
      Sample ID: #7
      Sample ID: #8
    Article ID: #4
      Sample ID: #9
    

    The disadvantage of this approach is that you’re having to enumerate the results list several times. If you know this list will always be small, then that’s not such a big deal. If the list will be very large, then you might want to come up with a better way to return the data.

    EDIT

    To make the code more efficient, you could group by InspectorArticle.ID and then create a Dictionary keyed by the ID, and containing the original InspectorArticle and the associated InspectorSamples.

    var articles = results.GroupBy(g => g.obj2.ID)
                          .ToDictionary(k => k.Key, v => new { 
                              InspectorArticle = v.Select(s => s.obj2).First(), 
                              InspectorSamples = v.Select(s => s.obj3) });
    
    foreach(var article in articles.OrderBy(a => a.Key).Select(kv => kv.Value))
    {
        Console.WriteLine("Article ID: #{0}", article.InspectorArticle.ID);
        foreach(var sample in article.InspectorSamples)
        {
            Console.WriteLine("\tSample ID: #{0}", sample.ID);
        }
    }
    

    The code above will yield the same results as my first example, but for longer lists of articles and samples will be more efficient since it will only enumerate the entire list once when building the Dictionary.

    Please note that I keyed the Dictionary off of the ID property because I didn’t supply an IEqualityComparer to the GroupBy method. If you would rather key by the InspectorArticle object itself, you would need to make sure that two different InspectorArticle instances with the same ID are viewed as equal, which can be done if you create an IEqualityComparer and pass it into the GroupBy method.

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

Sidebar

Related Questions

So I have a LINQ query below: var data= (from p in _db.P join
I have a fairly complex query where I am filtering results with a LIKE
I have a fairly complex LINQ to Entities query I'd like to try compiling
I have a very complex Linq to SQL query that returns a result set
I have a complex object and when I use a linq query without the
I am trying to write a slightly complex LINQ to SQL query. I have
I have a complex LINQ to SQL to query, which joins onto two tables
I have a complex query that I'm trying to reproduce in LINQ to Entities,
I have a pretty complex linq statement I need to access for different methods.
What is the format for databinding to a complex object? I have a linq

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.