Horje
how to map datatable to list in c# Code Example
how to map datatable to list in c#
public List<T> ConvertToList<T>(DataTable dt)
{
    var columnNames = dt.Columns.Cast<DataColumn>()
            .Select(c => c.ColumnName)
            .ToList();
    var properties = typeof(T).GetProperties();
    return dt.AsEnumerable().Select(row =>
    {
        var objT = Activator.CreateInstance<T>();
        foreach (var pro in properties)
        {
            if (columnNames.Contains(pro.Name))
            {
                 PropertyInfo pI = objT.GetType().GetProperty(pro.Name);
                 pro.SetValue(objT, row[pro.Name] == DBNull.Value ? null : Convert.ChangeType(row[pro.Name], pI.PropertyType));
            }
        }
        return objT;
   }).ToList();
}




Csharp

Related
git check for uncommitted changes Code Example git check for uncommitted changes Code Example
c# multiply string Code Example c# multiply string Code Example
c# call main method Code Example c# call main method Code Example
remove first character in a string c# Code Example remove first character in a string c# Code Example
How can I cast string to enum? Code Example How can I cast string to enum? Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
9