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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:52:17+00:00 2026-05-16T20:52:17+00:00

I have an array of Car objects and using the following piece of code

  • 0

I have an array of Car objects and using the following piece of code I create an XML Document from these objects. I have set up a counter variable i to be able to index the Car elements in the document. Is there a different way of obtaining the index of the currently processed element?

        int i = 0;
        XDocument doc =
            new XDocument(
                new XElement(
                    "Inventory",
                    from car in cars
                    select
                        new XElement("Car",
                            new XAttribute("ID", ++i), //<<== index here
                            new XElement("Color", car.Color),
                            new XElement("Make", car.Make),
                            new XElement("PetName", car.PetName)
                        )
                )
            );

The approach I have taken works fine, I am just wondering whether there’s a magic word or extension method that will yield the index without my incrementing a counter variable?

  • 1 1 Answer
  • 1 View
  • 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-16T20:52:18+00:00Added an answer on May 16, 2026 at 8:52 pm

    Yup – don’t use a query expression; use the overload of Select which provides an index. This replaces your query expression:

    cars.Select((car, index) =>
        new XElement("Car",
            new XAttribute("ID", index),
            new XElement("Color", car.Color),
            new XElement("Make", car.Make),
            new XElement("PetName", car.PetName)
        ))
    

    There are various overloads which aren’t supported in query expressions – it’s definitely worth being familiar with both “dot notation” (or whatever you want to call it) and query expressions.

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

Sidebar

Related Questions

I have an array of Car objects I want to conver them to a
i have an array of objects (Car[] for example) and there is an IsAvailable
I have the following array: NSString *testString = @1|Car|Red|Ford; NSArray *testArray = [testString componentsSeparatedByString:@|];
I have this piece of code: return json_decode($body); This return is from a function
I'm trying to create an array of JS objects from a PHP array, but
Say I have an array with the following members: car_porsche car_mercedes car_toyota motorcycle_suzuki motorcycle_honda
When I have array of objects, when I type the name of variable in
I have several string each containing a JSON representation of an array of objects.
I have an Array of AutoMobiles This array may contain SubClass Objects Truck or
Is there a way to pick a random object from an array of objects?

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.