i




 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dataset to Generic List conversion in c#
01-02-2013, 01:13 PM (This post was last modified: 01-02-2013 01:14 PM by xpert.)
Post: #1
Not Solved Dataset to Generic List conversion in c#
Hello,
i am looking for the code to convert the dataset to generic list conversion in c#, my requirement is to pass the data from WCF service to silverlight. can somebody help me to complete my task.

Thanks


Quote
01-03-2013, 06:59 AM
Post: #2
Not Solved RE: Dataset to Generic List conversion in c#
Hi Xpert,

Sure there is a generic method to convert your DataSet to a compatible class List. For this you will need to have SAME Property Names and COMPATIBLE DataTypes in that class like the ones you have in your DataTable. For example if you have a class with name Employee like following:

Code:
[DataContract]
public class Employee
{
    [DataMember]
    public string Name { get; set; }
    [DataMember]
    public int Salary { get; set; }
    [DataMember]
    public int Age { get; set; }
    [DataMember]
    public string JoinDate { get; set; }
    [DataMember]
    public string Department { get; set; }
}

Then you will use it like this:

Code:
List<Employee> myList = (List<Employee>)DataFiller.ConvertTo<Employee>(DTEmployee);

Remember that your DataTable (DTEmployee) should have fields with all the above names,i.e. Name (varchar), Salary (int), Age (int), JoinDate (varchar) and Department (varchar) with compatble DataTypes.

To use the above technique, you need the following class in your Project. The code of that class is as follows:

Code:
using System.Collections.Generic;
using System.Reflection;
using System.Data;
using System;
using System.ComponentModel;

namespace TransM
{
    public class DataFiller
    {
        public static DataTable ConvertTo<T>(IList<T> list)
        {
            DataTable table = CreateTable<T>();
            Type entityType = typeof(T);
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entityType);

            foreach (T item in list)
            {
                DataRow row = table.NewRow();

                foreach (PropertyDescriptor prop in properties)
                {
                    row[prop.Name] = prop.GetValue(item);
                }

                table.Rows.Add(row);
            }

            return table;
        }

        public static IList<T> ConvertTo<T>(IList<DataRow> rows)
        {
            IList<T> list = null;

            if (rows != null)
            {
                list = new List<T>();

                foreach (DataRow row in rows)
                {
                    T item = CreateItem<T>(row);
                    list.Add(item);
                }
            }

            return list;
        }

        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 CreateItem<T>(DataRow row)
        {
            T obj = default(T);
            if (row != null)
            {
                obj = Activator.CreateInstance<T>();

                foreach (DataColumn column in row.Table.Columns)
                {
                    PropertyInfo prop = obj.GetType().GetProperty(column.ColumnName);
                    try
                    {
                        object value = row[column.ColumnName];
                        prop.SetValue
                            (obj, value.ToString(), null);
                    }
                    catch
                    {
                        // You can log something here
                        throw;
                    }
                }
            }

            return obj;
        }

        public static DataTable CreateTable<T>()
        {
            Type entityType = typeof(T);
            DataTable table = new DataTable(entityType.Name);
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entityType);

            foreach (PropertyDescriptor prop in properties)
            {
                table.Columns.Add(prop.Name, prop.PropertyType);
            }

            return table;
        }
    }
}

Hope this helps, Xpert... Tongue



View Asad Ali's profile on LinkedIn

Quote


Possibly Related Threads...
Thread: Author Replies Views: Last Post
  in dataset data is in byte praveen 3 724 01-02-2013 10:56 AM
Last Post: nisar87
  [Asp.net]  difference between DataReader, DataTable and DataSet Priya 5 30,710 11-08-2012 06:43 AM
Last Post: magnum_2007
  How to bind dataset to gridview Dharmesh Sharma 2 2,291 07-23-2012 05:07 AM
Last Post: raj2sekar1
  how to bind dataset to ddl? prasuna 4 2,055 07-23-2012 04:58 AM
Last Post: raj2sekar1
  Add next line(\n) in a generic List contains more than 100 characters gopal_nivas 1 1,047 07-02-2012 12:08 PM
Last Post: sharath



User(s) browsing this thread: 1 Guest(s)