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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:26:59+00:00 2026-05-20T10:26:59+00:00

I ran into an issue today and I have been stumped for some time

  • 0

I ran into an issue today and I have been stumped for some time in trying to get the results I am searching for.

I currently have a class that resembles the following:

public class InstanceInformation
{
     public string PatientID {get; set;}
     public string StudyID {get; set;}
     public string SeriesID {get; set;}
     public string InstanceID {get; set;}
}

I have a List<InstanceInformation> and I am trying to use LINQ (or whatever other means to generate paths (for a file-directory) based on this list that resemble the following:

PatientID/StudyID/SeriesID/InstanceID

My issue is the data is currently unstructured as it comes in the previously mentioned form (List) and I need a way to group all of the data with the following constraints:

  • Group InstanceIDs by SeriesID
  • Group SeriesIDs by StudyID
  • Group StudyIDs by PatientID

I currently have something that resembles this:

var groups = from instance in instances
             group instance by instance.PatientID into patientGroups
             from studyGroups in
                 (from instance in patientGroups
                   group instance by instance.StudyID)
                   from seriesGroup in
                       (from instance in studyGroups
                        group instance by instance.SeriesID)
                            from instanceGroup in
                                 (from instance in seriesGroup
                                  group instance by instance.InstanceID)
             group instanceGroup by patientGroups.Key;

which just groups all of my InstanceIDs by PatientID, and it’s quite hard to cull through all of the data after this massive grouping to see if the areas in between (StudyID/SeriesID) are being lost. Any other methods of solving this issue would be more than welcome.

This is primarily just for grouping the objects – as I would need to then iterate through them (using a foreach)

  • 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-20T10:27:00+00:00Added an answer on May 20, 2026 at 10:27 am

    I have no idea if the query you’ve come up with is the query you actually want or need, but assuming that it is, let’s consider the question of whether there is a better way to write it.

    The place you want to look is section 7.16.2.1 of the C# 4 specification, a portion of which I quote here for your convenience:


    A query expression with a continuation

    from ... into x ...
    

    is translated into

    from x in ( from ... ) ...
    

    Is that clear? Let’s take a look at a fragment of your query that I’ve marked with stars:

    var groups = from instance in instances
                 group instance by instance.PatientID into patientGroups
                 from studyGroups in
                     **** (from instance in patientGroups
                       group instance by instance.StudyID) ****
                       from seriesGroup in
                           (from instance in studyGroups
                            group instance by instance.SeriesID)
                                from instanceGroup in
                                     (from instance in seriesGroup
                                      group instance by instance.InstanceID)
                 group instanceGroup by patientGroups.Key;
    

    Here we have

    from studyGroups in ( from ... ) ...
    

    the spec says that this is equivalent to

    from ... into studyGroups ...
    

    so we can rewrite your query as

    var groups = from instance in instances
                 group instance by instance.PatientID into patientGroups
                 from instance in patientGroups
                 group instance by instance.StudyID into studyGroups
                 from seriesGroup in
                 **** (from instance in studyGroups
                      group instance by instance.SeriesID) ****
                          from instanceGroup in
                               (from instance in seriesGroup
                                group instance by instance.InstanceID)
                 group instanceGroup by patientGroups.Key;
    

    Do it again. Now we have

    from seriesGroup in (from ... ) ...
    

    and the spec says that this is the same as

    from ... into seriesGroup ...
    

    so rewrite it like that:

    var groups = from instance in instances 
                 group instance by instance.PatientID into patientGroups
                 from instance in patientGroups 
                 group instance by instance.StudyID into studyGroups
                 from instance in studyGroups
                 group instance by instance.SeriesID into seriesGroup
                 from instanceGroup in
                  ****     (from instance in seriesGroup
                       group instance by instance.InstanceID) ****
                 group instanceGroup by patientGroups.Key;
    

    And again!

    var groups = from instance in instances 
                 group instance by instance.PatientID into patientGroups
                 from instance in patientGroups 
                 group instance by instance.StudyID into studyGroups
                 from instance in studyGroups
                 group instance by instance.SeriesID into seriesGroup
                 from instance in seriesGroup
                 group instance by instance.InstanceID into instanceGroup
                 group instanceGroup by patientGroups.Key;
    

    Which I hope you agree is a whole lot easier to read. I would improve its readability more by changing the fact that “instance” is used half a dozen times to mean different things:

    var groups = from instance in instances 
                 group instance by instance.PatientID into patientGroups
                 from patientGroup in patientGroups 
                 group patientGroup by instance.StudyID into studyGroups
                 from studyGroup in studyGroups
                 group studyGroup by studyGroup.SeriesID into seriesGroups
                 from seriesGroup in seriesGroups
                 group seriesGroup by seriesGroup.InstanceID into instanceGroup
                 group instanceGroup by patientGroups.Key;
    

    Whether this is actually the query you need to solve your problem, I don’t know, but at least this one you can reason about without turning yourself inside out trying to follow all the nesting.

    This technique is called “query continuation”. Basically the idea is that the continuation introduces a new range variable over the query so far.

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

Sidebar

Related Questions

I ran into an interesting (and very frustrating) issue with the equals() method today
Totally revising this question, since I fixed the first issue but have ran into
I ran into an issue with an IIS web app shutting down an idle
I ran into an issue with something that I am probably just overlooking. I
This morning I ran into an issue with returning back a text string as
I recently ran into a issue where intermediate link betweeen a TCP server and
I just ran into an issue with Python's imaplib and Gmail's authentication mechanism: >>>
I ran into this today when unit testing a generic dictionary. System.Collections.Generic.Dictionary<int, string> actual,
We ran into an issue where our log table got far larger far faster
I recently ran into an issue that could easily be solved using modulus division,

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.