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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:58:45+00:00 2026-05-23T01:58:45+00:00

I’m currently trying to convert an IEnumerable<T> to a 2-dimensional array of type T2

  • 0

I’m currently trying to convert an IEnumerable<T> to a 2-dimensional array of type T2 using an extension method with generic types. You should also be able to choose which properties of T you want to include into that array.

Here’s what I got so far:

public static T2[][] ToMultidimensionalArray<T, T2>(this IEnumerable<T> enumerable, int count, params string[] propNames)
    {
        IEnumerator<T> enumerator = enumerable.GetEnumerator();
        T2[][] resultArray = new T2[count][];
        int i = 0;
        int arrLength = propNames.Length;
        while (enumerator.MoveNext())
        {
            resultArray[i] = new T2[arrLength];
            int j = 0;
            foreach(string prop in propNames)
            {
                resultArray[i][j] = ((T)enumerator.Current).//How do I access the properties?
                j++;
            }
            i++;
        }
        return resultArray;
    }

I’m having a problem accessing the properties of enumerator.Current within the foreach-Loop.

I’m using .NET-Framework 4.0.

Any input would be greatly appreciated.

Thanks,

Dennis

  • 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-23T01:58:46+00:00Added an answer on May 23, 2026 at 1:58 am

    In general, this problem can be solved using reflection:

    public static T2[][] ToMultidimensionalArray<T, T2>(
                                                    this IEnumerable<T> enumerable,
                                                    int count,
                                                    params string[] propNames)
    {
        T2[][] resultArray = new T2[count][];
        int i = 0;
        int arrLength = propNames.Length;
        foreach (var item in enumerable)
        {
            resultArray[i] = new T2[arrLength];
            int j = 0;
            foreach (string prop in propNames)
            {
                // Get the required property info using reflection
                var propertyInfo = typeof(T).GetProperty(prop);
                // Extract the getter method of the property
                var getter = propertyInfo.GetGetMethod();
                // Invoke the getter and get the property value
                var value = getter.Invoke(item, null);
                // Cast the value to T2 and store in the array
                resultArray[i][j] = (T2) value;
                j++;
            }
            i++;
        }
        return resultArray;
    }
    

    I understood the problem as having a Ts collection where these objects have properties of T2 type. The goal is to take the properties of each object and place them in a multidimensional array. Correct me if I’m wrong.

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

Sidebar

Related Questions

I am currently running into a problem where an element is coming back from
I want use html5's new tag to play a wav file (currently only supported
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I am trying to loop through a bunch of documents I have to put
I'm making a simple page using Google Maps API 3. My first. One marker
I have a JSP page retrieving data and when single or double quotes are

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.