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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:04:24+00:00 2026-05-16T01:04:24+00:00

I am trying to convert a DataTable to an IEnumerable. Where T is a

  • 0

I am trying to convert a DataTable to an IEnumerable. Where T is a custom type I created. I know I can do it by creating a List<T> but I was thinking if there is a slicker way to do it using IEnumerable. Here is what I have now:

private IEnumerable<TankReading> ConvertToTankReadings(DataTable dataTable)
{
    var tankReadings = new List<TankReading>();
    foreach (DataRow row in dataTable.Rows)
    {
        var tankReading = 
              new TankReading
              {
                  TankReadingsID = Convert.ToInt32(row["TRReadingsID"]),
                  TankID = Convert.ToInt32(row["TankID"]),
                  ReadingDateTime = Convert.ToDateTime(row["ReadingDateTime"]),
                  ReadingFeet = Convert.ToInt32(row["ReadingFeet"]),
                  ReadingInches = Convert.ToInt32(row["ReadingInches"]),
                  MaterialNumber = row["MaterialNumber"].ToString(),
                  EnteredBy = row["EnteredBy"].ToString(),
                  ReadingPounds = Convert.ToDecimal(row["ReadingPounds"]),
                  MaterialID = Convert.ToInt32(row["MaterialID"]),
                  Submitted = Convert.ToBoolean(row["Submitted"]),
              };

        tankReadings.Add(tankReading);
    }
    return tankReadings.AsEnumerable();
}

The key part being I am creating a List<T> then returning it using AsEnumerable().

  • 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-16T01:04:25+00:00Added an answer on May 16, 2026 at 1:04 am

    Nothing wrong with that implementation. You might give the yield keyword a shot, see how you like it:

    private IEnumerable<TankReading> ConvertToTankReadings(DataTable dataTable)
        {
            foreach (DataRow row in dataTable.Rows)
            {
                yield return new TankReading
                                      {
                                          TankReadingsID = Convert.ToInt32(row["TRReadingsID"]),
                                          TankID = Convert.ToInt32(row["TankID"]),
                                          ReadingDateTime = Convert.ToDateTime(row["ReadingDateTime"]),
                                          ReadingFeet = Convert.ToInt32(row["ReadingFeet"]),
                                          ReadingInches = Convert.ToInt32(row["ReadingInches"]),
                                          MaterialNumber = row["MaterialNumber"].ToString(),
                                          EnteredBy = row["EnteredBy"].ToString(),
                                          ReadingPounds = Convert.ToDecimal(row["ReadingPounds"]),
                                          MaterialID = Convert.ToInt32(row["MaterialID"]),
                                          Submitted = Convert.ToBoolean(row["Submitted"]),
                                      };
            }
    
        }
    

    Also the AsEnumerable isn’t necessary, as List<T> is already an IEnumerable<T>

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

Sidebar

Related Questions

Trying to convert the following but am having difficulty. Can anyone see where I'm
I am trying to convert datatable to List. Could you please help me with
I am trying to convert a generic collection (List) to a DataTable. I found
I'm trying to convert a DataRow to a DataTable , but I'm getting errors.
Im trying to convert unixtime to date but the results im getting are wrong
I am trying to write a generic method that will convert a DataTable to
I am trying to AVERAGE 6 different fields on a DataTable, but without grouping.
Im trying to convert the a day to a link if there are any
I have generic list ... I am trying to convert it to a data
I'm trying to populate a List with a column of a DataTable in order

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.