EntityFramework6写的数据访问框架之三DatabaseExtension
来源:互联网 发布:正元恒邦数据是假的吗 编辑:程序博客网 时间:2024/05/19 22:06
using System;using System.Collections;using System.Collections.Generic;using System.Configuration;using System.Data;using System.Data.Entity;using System.Data.SqlClient;using System.Linq;using System.Linq.Expressions;using System.Reflection;using System.Text;using System.Threading.Tasks;namespace MyTest{ public static class DatabaseExtension { public static List<T> ToList<T>(this DataTable dt) where T : class,new() { Type t = typeof(T); PropertyInfo[] propertys = t.GetProperties(); List<T> lst = new List<T>(); string typeName = string.Empty; foreach (DataRow dr in dt.Rows) { T entity = new T(); foreach (PropertyInfo pi in propertys) { typeName = pi.Name; if (dt.Columns.Contains(typeName)) { if (!pi.CanWrite) continue; object value = dr[typeName]; if (value == DBNull.Value) continue; if (pi.PropertyType == typeof(string)) { pi.SetValue(entity, value.ToString(), null); } else if (pi.PropertyType == typeof(int) || pi.PropertyType == typeof(int?)) { pi.SetValue(entity, int.Parse(value.ToString()), null); } else if (pi.PropertyType == typeof(DateTime?) || pi.PropertyType == typeof(DateTime)) { pi.SetValue(entity, DateTime.Parse(value.ToString()), null); } else if (pi.PropertyType == typeof(float) || pi.PropertyType == typeof(float?)) { pi.SetValue(entity, float.Parse(value.ToString()), null); } else if (pi.PropertyType == typeof(double) || pi.PropertyType == typeof(double?)) { pi.SetValue(entity, double.Parse(value.ToString()), null); } else if (pi.PropertyType == typeof(byte) || pi.PropertyType == typeof(byte?)) { pi.SetValue(entity, byte.Parse(value.ToString()), null); } else if (pi.PropertyType == typeof(Int16) || pi.PropertyType == typeof(Int16?)) { pi.SetValue(entity, Int16.Parse(value.ToString()), null); } else { pi.SetValue(entity, value, null); } } } lst.Add(entity); } return lst; } public static IEnumerable<TElement> ProcedureQuery<TElement>(this Database db, string sp, params SqlParameter[] parameters) { string sql = ""; for (int i = 0; i < parameters.Length; i++) { sql += " " + parameters[i].ParameterName + " = {" + i + "},"; } sql = "EXEC " + sp + " " + sql.TrimEnd(','); return db.SqlQuery<TElement>(sql, parameters.Select(p => p.Value).ToArray()); } public static IQueryable<TEntity> WhereIn<TEntity, TValue>(this IQueryable<TEntity> source, Expression<Func<TEntity, TValue>> valueSelector, IEnumerable<TValue> values) { if (null == valueSelector) { throw new ArgumentNullException("valueSelector"); } if (null == values) { throw new ArgumentNullException("values"); } ParameterExpression p = valueSelector.Parameters.Single(); if (!values.Any()) { return source; } var equals = values.Select(value => (Expression)Expression.Equal(valueSelector.Body, Expression.Constant(value, typeof(TValue)))); var body = equals.Aggregate<Expression>((accumulate, equal) => Expression.Or(accumulate, equal)); return source.Where(Expression.Lambda<Func<TEntity, bool>>(body, p)); } }}
0 0
- EntityFramework6写的数据访问框架之三DatabaseExtension
- EntityFramework6写的数据访问框架之二BaseDAL
- EntityFramework6写的数据访问框架之四使用
- EntityFramework6写的数据访问框架之五事务
- EntityFramework6写的数据访问框架之一 BaseDb
- EntityFramework6中管理DbContext的正确方式(三)【三种DbContext用法】
- Spring框架学习之三(类型数据的注入)
- Yii框架之数据访问对象 (DAO)
- Windows DNA 之三 数据访问逻辑
- 自己写的数据访问层
- 自己写的一个数据访问类
- Java专家之路(二)---数据访问和数据持久化框架的总结
- spring的数据访问框架的原理
- 原创的java数据访问框架
- 遵循Java EE标准规范的开源GIS服务平台之三:数据发布与访问
- 遵循Java EE标准规范的开源GIS服务平台之三:数据发布与访问
- ibatisNet 数据访问框架
- 数据访问框架
- EntityFramework6写的数据访问框架之二BaseDAL
- java 序列化实现对象的深拷贝
- SQL-事务基础知识
- jq选择器-1
- could not initialize proxy - no Session
- EntityFramework6写的数据访问框架之三DatabaseExtension
- android自动弹出软键盘
- View如何使用对话框中的变量
- RMAN-06183: datafile or datafile copy +DATA_ERP/ap_media35 (file number 27) larger than MAXSETSIZE
- JavaSE学习53:细说多线程之内存可见性
- python类的简单定义
- 如何在Android Studio项目中导入开源库?
- Django开发(一)
- PHP 魔术引号