因为觉得一般使用DataRow获取行数据时使用字符串 会有太多的不确定和类型判断所以想自己扩展一下
最后成果
public class Model
{
public int objUserID { get; set; }
public string FirstName { get; set; }
}
class Program
{
static void Main(string[] args)
{
DataRow r = null;
var contact= r.GetContact<Model>();//声明一个DataRow的链接
//获取值类型
var id=contact.GetValue(s => s.objUserID,s=>int.Parse(s));
//获取引用类型
var name = contact.GetClassValue(s => s.FirstName);
//直接获取int类型
id = contact.GetValueByDBNull(s => s.objUserID);
System.Data.SqlClient.SqlDataReader reader = null;
contact = reader.GetContact<Model>();//声明一个SqlDataReader的链接
//获取值类型
id = contact.GetValue(s => s.objUserID);
//直接获取int类型
id = contact.GetValueByDBNull(s => s.objUserID);
}
}开始定义一部分接口,公开外部可访问的方法
public interface ContactBase<T>
{
/// <summary>
/// 获取值类型
/// </summary>
TResult GetValue<TResult>(System.Linq.Expressions.Expression<Func<T, TResult>> exp, Func<string, TResult> convert=null);
/// <summary>
/// 获取引用类型
/// </summary>
TResult GetClassValue<TResult>(System.Linq.Expressions.Expression<Func<T, TResult>> exp,Func<object,TResult> convert=null)
where TResult :class;
/*获取指定类型*/
int GetValueByDBNull(System.Linq.Expressions.Expression<Func<T, int>> exp);
int GetValueByDBNull(System.Linq.Expressions.Expression<Func<T, double>> exp);
int GetValueByDBNull(System.Linq.Expressions.Expression<Func<T, string>> exp);
} public abstract class ContactTools<T,TData>:ContactBase<T>
{
/// <summary>
/// 存放row或reader的值
/// </summary>
private TData _data;
protected ContactTools(TData data)
{
this._data = data;
}
/// <summary>
/// 通过lambda获取row或reader值
/// </summary>
private object GetDBValue<TResult>(System.Linq.Expressions.Expression<Func<T, TResult>> exp, Func<string, object> getFunc)
{
if (exp == null || getFunc == null)
return default(TResult);
var member = exp.Body as System.Linq.Expressions.MemberExpression;
var name = member.Member.Name;
return getFunc(name);
}
/// <summary>
/// 需要继承的改变
/// </summary>
public abstract virtual object GetData(TData data,string name);
public TResult GetValue<TResult>(System.Linq.Expressions.Expression<Func<T, TResult>> exp, Func<string, TResult> convert = null)
{
var value = this.GetDBValue(exp,s=>this.GetData(this._data,s));
if (value == DBNull.Value)
return default(TResult);
if (string.IsNullOrEmpty(value.ToString()))
return default(TResult);
if (convert == null)
return (TResult)value;
else
return convert(value.ToString());
}
public TResult GetClassValue<TResult>(System.Linq.Expressions.Expression<Func<T, TResult>> exp, Func<object, TResult> convert = null)
where TResult : class
{
var value = this.GetDBValue(exp, s => this.GetData(this._data, s));
if (value == DBNull.Value)
return null;
if (convert == null)
return value as TResult;
else
return convert(value);
}
public int GetValueByDBNull(System.Linq.Expressions.Expression<Func<T, int>> exp)
{
return this.GetValue(exp, s => int.Parse(s));
}
public double GetValueByDBNull(System.Linq.Expressions.Expression<Func<T, double>> exp)
{
return this.GetValue(exp, s => double.Parse(s));
}
public string GetValueByDBNull(System.Linq.Expressions.Expression<Func<T, string>> exp)
{
var str =this.GetClassValue(exp);
return str == null ? string.Empty : str;
}
}
public class DataRowContact<T> : ContactTools<T, DataRow>
{
public DataRowContact(DataRow row):base(row){}
public override object GetData(DataRow data, string name)
{
return data[name];
}
} public class DataReaderContact<T> : ContactTools<T, System.Data.SqlClient.SqlDataReader>
{
public DataReaderContact(System.Data.SqlClient.SqlDataReader reader) : base(reader) { }
public override object GetData(System.Data.SqlClient.SqlDataReader data, string name)
{
return data[name];
}
} public static class MyLinq
{
public static ContactBase<T> GetContact<T>(this DataRow row)
{
return new DataRowContact<T>(row);
}
public static ContactBase<T> GetContact<T>(this System.Data.SqlClient.SqlDataReader reader)
{
return new DataReaderContact<T>(reader);
}
}
随手写的一个对DataRow和DataSqlReader的扩展方法
原文地址:http://blog.csdn.net/qq6648208281/article/details/45503509