Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

Convert dataset to list of objects c#

public static class Extensions
    {
        public static List<T> ToList<T>(this DataTable table) where T : new()
        {
            IList<PropertyInfo> properties = typeof(T).GetProperties().ToList();
            List<T> result = new List<T>();

            foreach (var row in table.Rows)
            {
                var item = CreateItemFromRow<T>((DataRow)row, properties);
                result.Add(item);
            }

            return result;
        }

        private static T CreateItemFromRow<T>(DataRow row, IList<PropertyInfo> properties) where T : new()
        {
            T item = new T();
            foreach (var property in properties)
            {
                if (property.PropertyType == typeof(System.DayOfWeek))
                {
                    DayOfWeek day = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), row[property.Name].ToString());
                    property.SetValue(item,day,null);
                }
                else
                {
                    if(row[property.Name] == DBNull.Value)
                        property.SetValue(item, null, null);
                    else
                    {
                        if (Nullable.GetUnderlyingType(property.PropertyType) != null)
                        {
                            //nullable
                            object convertedValue = null;
                            try
                            {
                                convertedValue = System.Convert.ChangeType(row[property.Name], Nullable.GetUnderlyingType(property.PropertyType));
                            }
                            catch (Exception ex)
                            {
                            }
                            property.SetValue(item, convertedValue, null);
                        }
                        else
                            property.SetValue(item, row[property.Name], null);
                    }
                }
            }
            return item;
        }
    }
Comment

PREVIOUS NEXT
Code Example
Typescript :: angular currency pipe pt-br as variable 
Typescript :: create custom objects for user in firebase 
Typescript :: footer credits with jquery date time 
Typescript :: typescript declare process.env 
Typescript :: defining component layout next ts 
Typescript :: typescript recursive types 
Typescript :: check anagramm in typescript 
Typescript :: give all element in a list starts with string 
Typescript :: typescript interface property multiple types 
Typescript :: plot multiple plots in r 
Typescript :: export interface typescript 
Typescript :: react-excel-renderer typescript 
Typescript :: input type file in html events angular 
Typescript :: basic variable types in typescript 
Typescript :: typescript one of the array items 
Typescript :: google_fonts pub.de 
Typescript :: comments visual studio code html 
Typescript :: compare two lists and remove duplicates java 
Typescript :: gamemanager unity resets after reloading scene 
Typescript :: beziere curve function 
Typescript :: get and set in typescript 
Typescript :: declare array typescript 
Typescript :: startswith multiple arguments python 
Typescript :: useCallback hook to fix useEffect re-render warning on function dependency 
Typescript :: mongodb move documents to another collection 
Typescript :: how-do-i-navigate-to-a-parent-route-from-a-child-route 
Typescript :: how to reset windows update components in windows 
Typescript :: software for checking open ports of IP 
Typescript :: google places auto-complete 
Typescript :: import luno pricing to google sheets api 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =