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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:10:07+00:00 2026-05-27T01:10:07+00:00

I have some models setup as follows: public class Form { public int FormId

  • 0

I have some models setup as follows:

public class Form
{
    public int FormId { get; set; }
    public virtual ICollection<SubForm> SubForms{ get; set; }
}
public class SubForm
{
    public int SubFormId { get; set; }
    public virtual Form Form {get; set;}
}

I have a list like this:

List<SubForm> subForms; <-- already populated, each subform has a form parent.

How do I get it in a format like this optimally (lambda preferred)

List<Form> formsWithChildren;
  • 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-27T01:10:07+00:00Added an answer on May 27, 2026 at 1:10 am

    You can group by the FormId then assign the grouped subforms to each new Form item as follows:

    var formsWithChildren = subForms.GroupBy(s => s.Form.FormId)
                                    .Select(g => new Form
                                    {
                                        FormId = g.Key,
                                        SubForms = g.ToList()
                                    }).ToList();
    

    EDIT: if other properties exist then you would have to map them in the object initializer, but this could become tedious, and isn’t the best option if you intend to reuse this type of query in other scenarios. I suggest creating a new Form object and mapping it using AutoMapper.

    If you decide to use AutoMapper the approach would be similar to this:

    var formsWithChildren = new List<Form>();
    foreach (var item in subForms.GroupBy(s => s.Form))
    {
        Form f = Mapper.Map<Form, Form>(item.Key);
        f.SubForms = item.Select(s => s).ToList();
        formsWithChildren.Add(f);
    }
    

    To learn more refer to the AutoMapper site.

    Alternately, you could take an approach as follows, although this modifies the existing items so be aware of that.

    var formsWithChildren = new List<Form>();
    foreach (var item in subForms.GroupBy(s => s.Form))
    {
        // re-use existing Form from grouping so it retains its assigned values
        Form f = item.Key;
        f.SubForms = item.Select(s => s).ToList();
        formsWithChildren.Add(f);
    }
    

    In both approaches shown above you will need to implement Equals on the Form class in order to GroupBy the Form instead of FormId:

    public class Form
    {
        public int FormId { get; set; }
        public virtual ICollection<SubForm> SubForms{ get; set; }
    
        public override bool Equals(object obj)
        {
            if (obj == null)
                return false;
    
            Form f = obj as Form;            
            return this.Equals(f);
        }
    
        public bool Equals(Form f)
        {
            if (f == null)
                return false;
    
            return this.FormId == f.FormId;
        }
    
        public override int GetHashCode()
        {
            return this.FormId.GetHashCode();
        }
    }
    

    You could extend the equality and GetHashCode methods to use the properties to determine uniqueness. Currently it only relies on the FormId.

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

Sidebar

Related Questions

Lets say I have some view models set up as follows: public class User
I have a model that is set up as follows: class Log(models.Model): name =
I have setup my schedule.rb file as follows. set :cron_log, /log/cron_log.log if Rails.env.development? every
I have set up a few models as follows: Page Gallery Image Gallery and
I have the models set up like this: class ParentModel(models.Model): some_col = models.IntegerField() some_other
I have the following domain model setup using EF code first: public class User
I have some models; Vocabularies (tags lists), Labels (tags) and different articles types. These
I have some models witch are using Doctrine nestedset feature. I want to add
Let's say I have some models: User , Post , and Vote . A
I have a Rails app with some basic models. The website displays data retrieved

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.