Last active
August 31, 2020 20:06
-
-
Save rvegajr/b6972a73a378428f2e3b79c5afeb1138 to your computer and use it in GitHub Desktop.
FastMember DataTable To Typed Object List
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class DatabaseExtensions | |
{ | |
static Regex underscore = new Regex(@"(^|_)(.)"); | |
static string convertName(string s) | |
{ | |
return underscore.Replace(s.ToLower(), m => m.Groups[0].ToString().ToUpper().Replace("_", "")); | |
} | |
static T ToObject<T>(this IDataRecord r) where T : new() | |
{ | |
var indexMembers = new Dictionary<string, Member>(); | |
T obj = new T(); | |
var accessor = TypeAccessor.Create(typeof(T)); | |
var members = accessor.GetMembers(); | |
foreach (var item in members) indexMembers.Add(item.Name, item); | |
for (int i = 0; i < r.FieldCount; i++) | |
{ | |
var name = r.GetName(i); | |
if (r.GetName(i).Equals("RSRC_ID")) | |
{ | |
var k = 10; | |
} | |
if (indexMembers.ContainsKey(name)) | |
{ | |
var PropertyType = indexMembers[name].Type; | |
if (r[i].GetType().Name.Equals("DBNull")) | |
{ | |
accessor[obj, name] = null; | |
} | |
else if (PropertyType == r[i].GetType()) | |
{ | |
accessor[obj, name] = r[i]; | |
} | |
else | |
{ | |
if (PropertyType.GenericTypeArguments.Contains(r[i].GetType())) | |
{ | |
accessor[obj, name] = r[i]; | |
} | |
else | |
{ | |
var c = TypeDescriptor.GetConverter(r[i]); | |
if (PropertyType.IsGenericType) PropertyType = PropertyType.GenericTypeArguments[0]; | |
if (c.CanConvertTo(PropertyType)) | |
{ | |
accessor[obj, name] = c.ConvertTo(r[i], PropertyType); | |
} | |
else | |
{ | |
try | |
{ | |
if (r[i].GetType().Name.Equals("Int64")) | |
accessor[obj, name] = System.Int64.Parse(r[i].ToString()); | |
if ((indexMembers[name].Type.Name.Contains("Decimal")) || (indexMembers[name].Type.GenericTypeArguments.First().Name.Contains("Decimal"))) | |
accessor[obj, name] = r[i].ToDecimal(); | |
else | |
accessor[obj, name] = System.Convert.ChangeType(r[i], indexMembers[name].Type); | |
} | |
catch (System.Exception ex) | |
{ | |
throw new System.Exception(string.Format("Could not conver field {0} of type {1} to {2}", name, r[i].GetType().Name, indexMembers[name].Type.Name), ex); | |
} | |
} | |
} | |
} | |
} | |
} | |
return obj; | |
} | |
static string DRToJson(this IDataRecord r) | |
{ | |
JObject returnJson = new JObject(); | |
for (int i = 0; i < r.FieldCount; i++) | |
{ | |
var name = r.GetName(i); | |
var c = TypeDescriptor.GetConverter(r[i]); | |
if (c.CanConvertTo(r[i].GetType())) | |
returnJson.Add(new JProperty(name, r[i])); | |
} | |
return returnJson.ToString(); | |
} | |
public static string ToJson(this IDataReader r) | |
{ | |
var sb = new StringBuilder(); | |
sb.Append("["); | |
var rsCount = 0; | |
do | |
{ | |
var recCount = 0; | |
sb.Append(((rsCount > 0) ? ",[" : "[")); | |
while (r.Read()) | |
{ | |
if (recCount > 0) sb.Append(","); | |
sb.Append(r.DRToJson()); | |
recCount++; | |
} | |
sb.Append("]"); | |
rsCount++; | |
} while (r.NextResult()); | |
sb.Append("]"); | |
return sb.ToString(); | |
} | |
public static IEnumerable<T> GetObjects<T>(this IDbCommand c) where T : new() | |
{ | |
using (IDataReader r = c.ExecuteReader()) | |
{ | |
while (r.Read()) | |
{ | |
yield return r.ToObject<T>(); | |
} | |
} | |
} | |
public static IEnumerable<T> GetObjects<T>(this IDataReader r) where T : new() | |
{ | |
while (r.Read()) | |
{ | |
yield return r.ToObject<T>(); | |
} | |
} | |
public static IEnumerable<T> GetObjectsNextRS<T>(this IDataReader r) where T : new() | |
{ | |
if (r.NextResult()) | |
{ | |
while (r.Read()) | |
{ | |
yield return r.ToObject<T>(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment