Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

Browse by Tags · View All
SQL Server 119
#SQLServer 88
Oracle 70
#SQL SERVER 35
BRH 31
SQL Server 2012 29
denali 23
#TSQL 19
TSQL 19
C# 15

Archive · View All
October 2011 31
November 2011 30
September 2011 30
August 2011 18
December 2011 15
July 2011 13
June 2011 8
May 2012 4
April 2012 3
January 2010 3

Convert a generic list to DataSet

Mar 23 2012 9:50PM by Niladri Biswas   

The below extension method will help us to convert a generic list to DataSet

public static class DataSetExtension
    {
       public static DataSet ToDataSet<T>(this List<T> list)
       {
           Type type = typeof(T);
           DataSet ds = new DataSet();
           DataTable dt = new DataTable(type.Name);
           ds.Tables.Add(dt);

           var propertyInfos = type.GetProperties().ToList();

           //For each property of generic List (T), add a column to table
           propertyInfos.ForEach(propertyInfo =>
           {
               Type columnType = Nullable.GetUnderlyingType(propertyInfo.PropertyType) ?? propertyInfo.PropertyType;
               dt.Columns.Add(propertyInfo.Name, columnType);
           });

           //Visit every property of generic List (T) and add each value to the data table 
           list.ForEach(item =>
           {
               DataRow row = dt.NewRow();
               propertyInfos.ForEach(
                                      propertyInfo =>
                                      row[propertyInfo.Name] = propertyInfo.GetValue(item, null) ?? DBNull.Value
                                    );
               dt.Rows.Add(row);
           });

           //Return the dataset
           return ds;
       }
    }
How to use this?
class Program
    {
        static void Main(string[] args)
        {
            List lstString = new List<Person>();
            Enumerable.Range(1, 10).ToList().ForEach(i => lstString.Add(
                            new Person { PersonId = i
                                        , PersonName = string.Concat("Name", i) 
                                        , Sex = i%2 ==0 ? "M":"F"
                                        , Salary  = 100 + i
                            }));

            var res = lstString.ToDataSet();
        }
    }

    public class Person
    {
        public int PersonId { get; set; }
        public string PersonName { get; set; }
        public string Sex { get; set; }
        public decimal Salary { get; set; }
    }

Hope this helps

Tags: C#, genericlist, dataset,


Niladri Biswas
7 · 21% · 6720
6
 
0
Lifesaver
 
0
Refreshed
 
 
0
Incorrect



Submit

2  Comments  

  • I have tried to do same thing in the past using reflection. but i have not used LINQ and lamda expression.

    Your code is really very clear and concise.

    Thanks Niladri...plz keep it up...

    commented on Feb 16 2013 12:15AM
    Krishnrajsinh Rana
    138 · 1% · 362
  • Thanks Niladri. This is very useful for us. Recently I encountered this requirement.

    commented on Feb 16 2013 2:52AM
    Ramireddy
    3 · 40% · 12972

Your Comment


Sign Up or Login to post a comment.

"Convert a generic list to DataSet" rated 5 out of 5 by 6 readers
Convert a generic list to DataSet , 5.0 out of 5 based on 6 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]