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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:57:58+00:00 2026-05-11T05:57:58+00:00

public static IList<T> ConvertTo<T>(DataTable table) { if (table == null) { return null; }

  • 0
    public static IList<T> ConvertTo<T>(DataTable table)     {         if (table == null)         {             return null;         }          List<DataRow> rows = new List<DataRow>();          foreach (DataRow row in table.Rows)         {             rows.Add(row);         }          return ConvertTo<T>(rows);     }      public static T ConvertItem<T>(DataTable table)     {         T obj = default(T);         if (table != null && table.Rows.Count > 0)         {             obj = CreateItem<T>(table.Rows[0]);         }         return obj;     }       public static T CreateItem<T>(DataRow row)     {         T obj = default(T);         if (row != null)         {             obj = Activator.CreateInstance<T>();             Type entityType = typeof(T);             PropertyInfo[] properties = entityType.GetProperties();              for (int i = 0; i < properties.Length; i++)             {                 object[] customAttributes = properties[i].GetCustomAttributes(typeof(ColumnAttributes), false);                 ColumnAttributes dataField = null;                 if (null != customAttributes && customAttributes.Length > 0 && null != (dataField = customAttributes[0] as ColumnAttributes))                 {                     if (row.Table.Columns.Contains(dataField.FieldName) && !row[dataField.FieldName].GetType().FullName.Equals('System.DBNull'))                     {                         properties[i].SetValue(obj, row[dataField.FieldName], null);                     }                 }             }         }         return obj;     } 

Thats the only thing we can think of right now is that we must be doing something where we need to Garbage collect Ourselves?

Thoughts?

Why we think there might be a leak?:

We are getting Out of Memory Errors. If a Page does not require business logic to use this type of conversion, the II6 process does not grow, but when we hit a page that uses it, it grows.

We are currently getting ANTS Profiler to give us more details.

  • 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. 2026-05-11T05:57:59+00:00Added an answer on May 11, 2026 at 5:57 am

    That won’t be an actual leak, but it could be stressing things unnecessarily…

    How many rows are you working over? Note that reflection is a pain, and that every call to things like GetCustomAttributes can return a new array (so you want to do that once, not once per-property-per-row).

    Personally, I’d pre-construct the work I intend to do… something like below.

    Note that if I was doing this lots, I’d either switch to HyperDescriptor, or if .NET 3.5 was an option, maybe a compiled Expression. Since DataTable isn’t strongly typed, HyperDescriptor would be a logical next step (for performance) after the below…

    sealed class Tuple<T1, T2> {     public Tuple() {}     public Tuple(T1 value1, T2 value2) {Value1 = value1; Value2 = value2;}     public T1 Value1 {get;set;}     public T2 Value2 {get;set;} } public static List<T> Convert<T>(DataTable table)     where T : class, new() {     List<Tuple<DataColumn, PropertyInfo>> map =         new List<Tuple<DataColumn,PropertyInfo>>();      foreach(PropertyInfo pi in typeof(T).GetProperties())     {         ColumnAttribute col = (ColumnAttribute)             Attribute.GetCustomAttribute(pi, typeof(ColumnAttribute));         if(col == null) continue;         if(table.Columns.Contains(col.FieldName))         {             map.Add(new Tuple<DataColumn,PropertyInfo>(                 table.Columns[col.FieldName], pi));         }     }      List<T> list = new List<T>(table.Rows.Count);     foreach(DataRow row in table.Rows)     {         if(row == null)         {             list.Add(null);             continue;         }         T item = new T();         foreach(Tuple<DataColumn,PropertyInfo> pair in map) {             object value = row[pair.Value1];             if(value is DBNull) value = null;             pair.Value2.SetValue(item, value, null);         }         list.Add(item);     }     return list;         } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 120k
  • Answers 120k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can not stretch a background image. Well at least… May 12, 2026 at 12:04 am
  • Editorial Team
    Editorial Team added an answer There are several approaches: You can normally see assembly code… May 12, 2026 at 12:04 am
  • Editorial Team
    Editorial Team added an answer Creating classes that do nothing except extending already existing collections… May 12, 2026 at 12:04 am

Related Questions

I am trying to convert a generic collection (List) to a DataTable. I found
I'm sure I've done this before, but can't find any example of it! Grrr...
I figured I could pass an IList<ChildClass> as an IEnumerable<ParentClass> , since obviously each
public static IList<T> LoadObjectListAll<T>() { ISession session = CheckForExistingSession(); var cfg = new NHibernate.Cfg.Configuration().Configure();

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.