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

The Archive Base Latest Questions

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

When building an object with the object initializer syntax, is there a way to

  • 0

When building an object with the object initializer syntax, is there a way to iterate over a list in one of the assignment statements and assign an output of the iteration to that property?

For example, I have a Person model which I’m converting into a Person view model (mostly because the Person model has lazy-loaded properties and is highly and potentially recursively dynamic, but the view is disconnected over the web and just needs a single Person instance to be serialized). I’m also using the nVentive Umbrella extensions for handy things like .ForEach() on enumerables. So to output my list of Person view models from my Person models, I convert them like this:

people.ForEach(p =>
  returnPeople.Add(
    new Person{
      FirstName = p.FirstName,
      ID = p.ID,
      LastName = p.LastName,
      MiddleName = p.MiddleName,
      NamePrefix = p.NamePrefix,
      NameSuffix = p.NameSuffix,
      ParentsID = p.Parents == null ? 0 : p.Parents.ID,
      PartnershipIDs = new List<int>(),
      Sex = p.Sex.ToString()
    }
  )
);

I’m iterating over the collection of Person models (people) and populating the collection of Person view models (returnPeople). Everything works fine, but I also want to populate that PartnershipIDs property in the same statement. Functionality such as:

//....
PartnershipIDs = p.Partnerships.ForEach(par => yield return par.ID)
//....

That, however, doesn’t work. The compiler doesn’t like the syntax. But is there a way to accomplish that functionality in this setup? Or do I have to initialize the Person view model with all but one field first, and then on the next line populate the remaining field?

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

    You need a simple projection:

    PartnershipIDs = p.Partnerships.Select(par => par.ID).ToList()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How does one go about programatically building a TemplateColumn object and adding it to
I am currently building a method that takes an object that is of type
I'm building a webservice which has a webmethod returning a Dataset object. I'm having
I have this helper that I am building: def myhelper(object, attributes = []) attributes.each
When building up a list of options in a select list using Javascript I'd
I'm using prototype 1.7 and building a class that will essentially take a list
I am building a sharepoint 2007 application. I am getting the Object Expected error
I am building a Calendar object. It is important that I have both a
Building on How Do You Express Binary Literals in Python , I was thinking
Building a client-side swing application what should be notified on a bus (application-wide message

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.