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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:54:00+00:00 2026-05-29T05:54:00+00:00

I have the following class which I am using to read in large amounts

  • 0

I have the following class which I am using to read in large amounts of data from an Access database.

public class ConnectToAccess
{
    private readonly string _connectionString;

    public ConnectToAccess(String connectionString)
    {
        _connectionString = connectionString;
    }

    public List<String> GetData(String sql)
    {
        var data = new List<String>();
        using (var connection = new OleDbConnection(_connectionString))
        {
            using (var command = connection.CreateCommand())
            {
                command.CommandText = sql;
                command.CommandType = CommandType.Text;
                connection.Open();
                using (var reader = command.ExecuteReader())
                {
                    if (reader != null && reader.HasRows)
                    while (reader.Read())
                    {
                        data.Add(reader["First Name"] + " " + reader["Last Name"]);
                    }
                }
            }
        }
        return data;
    }
}

As is, this code is working and is successfully pulling data in from the database. However, I would like to enhance the GetData() method to make it more dynamic. I would like it to somehow return a list of anonymous objects, where each object has properties relating to the columns of the dataset returned.

I’ve been coding in .Net for a while, but I’m still rather new at many concepts. I’m not quite sure how to create this list of anonymous objects that mirror the columns in the dataset most effectively. I’m also not sure what return type I would use in this case, I’m thinking maybe List. Then I suppose I would need to use reflection to pull the data out of those anonymous objects and transfer it into where it needs to go.

If anyone can help me with any significant part of this puzzle, I would be most obliged.

  • 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-29T05:54:02+00:00Added an answer on May 29, 2026 at 5:54 am

    You can’t have an anonymous type as a return type.
    Why not just return a DataTable. You can even use a DataAdapter to make the process a lot easier. It also gets you the schema.

    If you insist on getting objects for everything:

    public IEnumerable<T> GetData(String sql, Func<DataReader, T> selector)
    {
        //code elided
    
        while (reader.Read())
        {
            yield return selector(reader);
        }    
    }
    

    Now you can use it with a selector:

    var people = GetData("Select * from People", reader => new Person { Name = reader{"Name"], Age = reader["Age"] })
    people.Take(5); //first five records only
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following C# code, which pulls data from a database & populates
I have a class which loads an xml file using the following: Path.Combine( AppDomain.CurrentDomain.BaseDirectory,
Let's say I have the following class hierarchy: TaskViewer inherits from ListViewer<Task> which in
I have a class which has the following constructor public DelayCompositeDesigner(DelayComposite CompositeObject) { InitializeComponent();
Assume the following: we have class B, which is a private class nested inside
I have the following helper class (simplified): public static class Cache { private static
I have the following two classes: TcmTPDataPanel = class(TcmTPBasePanel) Database: TnxDatabase; Session: TnxSession; private
I have the following class which uses BinaryReader internally and implements IDisposable. class DisposableClass
I have the following class which handles my user logged in / logged out
I have the following tricky problem: I have implemented a (rather complicated) class which

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.