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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:08:28+00:00 2026-06-12T01:08:28+00:00

I have a Generic list of a class that I automatically convert it to

  • 0

I have a Generic list of a class that I automatically convert it to DataTable using Reflection and extension methods.Now I want to do it in reverse direction.I want to convert DataTable to List.Better to say I want help to write a method that expect a DataTable and a Type and automatically find property of that type(class) according to column name and assign value to object of that Type(class).like this psodu code:

private List<T> ConvertToList<T>(DataTable dt)
{
    List<string> AllColumns = // Get All Column Names of a DataTable
    for(int i=0;i<dt.Rows.Count;i++)
    {
        foreach(var item in AllColumns )
        {
             //Get Property According To **ITEM**
             //Get Data Of Rows[i][item] and assign it to T.property
        }
    }
}

How I can do this?


Edit 1)

I use answer of @Cuong Le like this:

var properties = typeof(CustomType).GetProperties().ToList();
List<CustomType> list = ConvertToList<CustomType>(dt, properties);

and :

private List<T> ConvertToList<T>(DataTable dt,List<PropertyInfo> fields) where T : class
{
    return dt.AsEnumerable().Select(Convert<T>(fields)).ToList();  <------
}

private T Convert<T>(DataRow row,List<PropertyInfo> fields) where T : class
{
    var properties = typeof(T).GetProperties().ToList();

    var objT = Activator.CreateInstance<T>();
    foreach (var pro in properties)
    {
        pro.SetValue(objT, row[pro.Name],null);
    }

    return objT;
} 

but in line I place an arrow in front of it I got this two errors:

No overload for method ‘Convert’ takes 1 arguments

and

The type arguments for method ‘System.Data.EnumerableRowCollectionExtensions.Select(System.Data.EnumerableRowCollection, System.Func)’ cannot be inferred from the usage. Try specifying the type arguments explicitly.

How I can solve this problem?

  • 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-06-12T01:08:29+00:00Added an answer on June 12, 2026 at 1:08 am

    Use AsEnumerable() method to support LINQ:

        private List<T> ConvertToList<T>(DataTable dt)
        {
            var columnNames = dt.Columns.Cast<DataColumn>()
                .Select(c => c.ColumnName)
                .ToList();
    
            var properties = typeof(T).GetProperties();
    
            return dt.AsEnumerable().Select(row =>
                {
                    var objT = Activator.CreateInstance<T>();
    
                    foreach (var pro in properties)
                    {
                        if (columnNames.Contains(pro.Name))
                            pro.SetValue(objT, row[pro.Name]);
                    }
    
                    return objT;
                }).ToList();
    
        }
    

    GetProperties searches for the properties of the current Type, using the specified binding constraints.

    The link in here: http://msdn.microsoft.com/en-us/library/kyaxdd3x.aspx

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

Sidebar

Related Questions

I have a project that is using a generic list of a custom class.
I have a generic class that I am using Reflection to pull out the
I have a class Team that holds a generic list: [DataContract(Name = TeamDTO, IsReference
I have a generic list class that implements a particular interface. The list items
I have a class that does some recursive merge sorting on a generic List,
Let's say I have a generic Object class, and a generic List class. I
I have a generic linked-list that holds data of type void* I am trying
i want to use below codes. So i have a generic list<MyClass> i think
I have created a subclass of a generic list so that I could implement
Winforms .net 3.5 app. In my app I have a generic class that looks

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.