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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:23:28+00:00 2026-05-18T04:23:28+00:00

I just discover LINQ so be comprehensive with me please! :-) So! I have

  • 0

I just discover LINQ so be comprehensive with me please! 🙂

So! I have a Data-tier who provide me datatables and i want to convert them into lists of objects. These objects are defined in a spécific layer DTO (Data transfer Objects).

How can I map every rows of my datatable into objects and put the all objects into a list? (today i make it “manually” field after field)
Is it possible with LINQ? I’ve heard about LINQ2Entities? am i right?

Thanks to help a beginner to understand…

  • 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-18T04:23:29+00:00Added an answer on May 18, 2026 at 4:23 am

    If the objects is not too complex you can use this:

    public static class DataTableExtensions
    {
       public static IList<T> ToList<T>(this DataTable table) where T : new()
       {
          IList<PropertyInfo> properties = typeof(T).GetProperties().ToList();
          IList<T> result = new List<T>();
    
          foreach (var row in table.Rows)
          {
             var item = CreateItemFromRow<T>((DataRow)row, properties);
             result.Add(item);
          }
    
          return result;
       }
    
       private static T CreateItemFromRow<T>(DataRow row, IList<PropertyInfo> properties) where T : new()
       {
           T item = new T();
           foreach (var property in properties)
           {
               property.SetValue(item, row[property.Name], null);
           }
           return item;
       }
    }
    

    With that in place you can now write: var list = YourDataTable.ToList<YourEntityType>().

    You can read about it here: http://blog.tomasjansson.com/convert-datatable-to-generic-list-extension/

    And it is an answer to a previous question: Convert DataTable to Generic List in C#

    EDIT: I should add that this is not linq, but some extension methods to DataTable I wrote. Also, it is working with the convention that the properties in the object you’re mapping with has the same name as in the DataTable. Of course this could be extended to read attributes on the properties or the method itself could take a simple Dictionary<string,string> that could be used to do the mapping. You could also extend it with some functionality that take a params string[] excludeProperties that could be used to exclude some of the properties.

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

Sidebar

Related Questions

Just curios if there's a way to have a rewrite for /static/index-discover.html look like
I just pasted some generated javadoc into an eclipse project, to discover none of
Just looking for a good PHP Image Library, I want to display images with
Just out of curiosity, how does iostream access the input-output system. (I have a
I just discover twitter-boostrap https://github.com/twbs/bootstrap and I'm wondering if there is a jquery library
I just discover the magic of using vi style in bash. Immediately, I'm trying
Is it possible to access Linq to SQL mapping data without a DataContext instance?
I just started using Solrnet in my application only to discover that using the
How might I discover which version of fabric I have installed - through the
I want to be able to discover if a Debian package has been installed

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.