Skip to main content

DataTable to List Model Converter | What is Generic Functions






what is Generic Functions | DataTable to List Model Converter 


Generics allow you to define the specification of the data type of programming elements. In other words, generics allow you to write a class or method that can work with any data type.

if you work on MVC and use a sql qurey for database intersection you need to fill value in list of model so this program convert DataTable to ListViewModel for you 


GenericFunctions.cs


using System;
using System.Collections.Generic;
using System.Data;
using System.Dynamic;
using System.Reflection;

namespace CommonFunctions
{
    public static class GenericFunctions
    {

        /// <summary>
        ///  pass DataTable in parameter and get data in list type model
        /// </summary>
        public static List<T> ConvertDataTable<T>(DataTable dt)
        {
            List<T> data = new List<T>();
            foreach (DataRow row in dt.Rows)
            {
                try
                {
                    T item = ConvertDataRow<T>(row);
                    data.Add(item);
                }
                catch (Exception ex) { }
            }
            return data;
        }

        /// <summary>
        ///  pass DataRow in parameter and get data in model
        /// </summary>
        public static T ConvertDataRow<T>(DataRow dr)
        {
            Type temp = typeof(T);
            T obj = Activator.CreateInstance<T>();

            foreach (DataColumn column in dr.Table.Columns)
            {
                try
                {
                    foreach (PropertyInfo pro in temp.GetProperties())
                    {
                        try
                        {
                            if (pro.Name == column.ColumnName)
                            {
                                var vvv = dr[column.ColumnName];
                                if (dr[column.ColumnName] is System.DBNull)
                                {
                                    // pro.SetValue(obj, string.Empty, null);
                                }
                                else
                                {
                                    pro.SetValue(obj, dr[column.ColumnName], null);
                                }
                            }
                            else
                            { continue; }
                        }
                        catch (Exception ex) { }
                    }
                }
                catch (Exception ex) { }
            }
            return obj;
        }

       
    }
}

If you like this blog so pls share and Write Comments about Your experience,
Thank You.

Comments

Popular posts from this blog

What is OOP (objects oriented programming) in C#

 What is OOP? in C# OOP is Object Oriented programming miens contain methods and data in objects it's called objects oriented programming(OOP) OOP Advantages Provides clear visibility and code for the programs easier to maintain, modify and debug Faster development easier and faster to execute create reusable code make your code neat and clean and easy to understand What is Class and objects in C# Class and object are the two main points of OOP (object oriented programming), when fruit is a class then Apple, Guava, Banana,  is object, when the individual objects are created they inherits all variables and method form the class, class is a template for the objects and object is instance of the class If you like this blog so pls share and  Write Comments  about Your experience, Thank You.

Make api in DotNet Core in 10sec | Create universal api for SQL server

Make API in Dot Net Core in 10sec | Create universal API for SQL server Universal API is great concept for app and angular developer need only connect to data base add table name and access table crud operation using API's  If you like this blog so pls share and  Write Comments  about Your experience, Thank You.

How to Optimize Your LinkedIn Profile as a Software Developer: Tips for Success

How to Optimize Your LinkedIn Profile as a Software Developer: Tips for Success To make your LinkedIn profile more attractive and discoverable as a software developer, you should focus on creating a compelling profile that showcases your skills, experience, and potential. Below are some specific strategies: 1. Professional Profile Picture Use a clear, professional photo where you look approachable and confident. A headshot with a neutral background works well. 2. Headline Your headline should be more than just your job title. Make it a powerful value proposition. Consider including: Your current role, tech stack, or specialization. Highlight key skills (e.g., "JavaScript | React | Node.js | Full-Stack Developer"). Optional: Include a personal tagline (e.g., "Building innovative web applications that solve real-world problems"). Example: "Full-Stack Developer | React, Node.js, Python | Passionate About Clean Code & Scalable Systems" 3. Summary (About Se...