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

The Archive Base Latest Questions

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

I am attempting to create a Dictionary from my DataTable. Currently i do this

  • 0

I am attempting to create a Dictionary from my DataTable. Currently i do this by first creating a IList and then looping through the List and adding them to a dictionary individually specifying the Primary key property of the resulting object in the list on a case by case basis.

I was wondering if this could be done using generics.

Ive currently got the following code, which doesnt compile or work:

public static IDictionary<T1, T2> ToDictionary<T1,T2>(this DataTable table) where T2 : new()
{
    IList<PropertyInfo> properties = GetPropertiesForType<T2>();
    IDictionary<T1,T2> result = new Dictionary<T1,T2>();
    Dictionary<string, int> propDict = GetPropertyDictionary(properties, table);
    T1 PK;
    foreach (var row in table.Rows)
    {
        var item = CreateDictItemFromRow<T2>((DataRow)row, properties, propDict);
        result.Add(item. item); //not sure here
    }
    return result;
}

Essentially i would like to call the following:

Dictionary<int, MyDBClass> Items = DataTable.ToDictionary<int,MyDBClass>();

or

Dictionary<Int16,MyDBClass> Items = DataTable.ToDictionary<Int16, MyDBClass>();

Im assuming somewhere i need to pass the primary key property name to this function. Failing that i can safely assume the first column in my Datatable contains the primary key (although its not safe to assume it is always an int (it maybe a short or byte)).

I also understand this could quite easily be done using LINQ, but i havent got that far in C# and wish to alter existing application.

Help is appreciated.

  • 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-29T23:37:47+00:00Added an answer on May 29, 2026 at 11:37 pm

    Hope this helps.

            public static class Extensions
            {
                public static Dictionary<TKey, TRow> TableToDictionary<TKey,TRow>(
                    this DataTable table,
                    Func<DataRow, TKey> getKey,
                    Func<DataRow, TRow> getRow)
                {
                    return table
                        .Rows
                        .OfType<DataRow>()
                        .ToDictionary(getKey, getRow);
                }
            }
    
    
    
            public static void SampleUsage()
            {
                DataTable t = new DataTable();
    
                var dictionary = t.TableToDictionary(
                    row => row.Field<int>("ID"),
                    row => new {
                        Age = row.Field<int>("Age"),
                        Name = row.Field<string>("Name"),
                        Address = row.Field<string>("Address"),
                    });
            }
    

    By the way, you need to include the assembly, System.Data.DataSetExtensions.dll, to use the Field extension method.

    If you have any other questions, don’t hesitate to ask.

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

Sidebar

Related Questions

I'm attempting to create a custom calendar control that inherits from ASP.Net's built in
I'm currently attempting to create a tabbed interface in a web application, and based
I am attempting to create a small application to collect data received from an
I'm attempting to create my first real C# application - a small pet project
I'm attempting to create my first WCF service. I've created the service and now
I am attempting to create a custom tableview where I have a list of
I'm attempting to create a gallery/gridview that is loaded with images from a specific
I am attempting to create new membership users in an Ektron CMS400.NET-based website by
I am attempting to create a REST API in PHP and I'd like to
I am attempting to create my own custom Autocomplete style dropdown control in c#

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.