linq to sql 扩展方法
2009-06-22 22:29
525 查看
linq to sql 给我们带来很多惊奇的地方,减少我们的代码,节省了我们的时间,但是为了更方便做了一些小的扩展,部分内容来自网络。
通过实体类插叙db.find<T>(T t):
public IQueryable<TEntity> Find<TEntity>(TEntity obj) where TEntity : class
{
//获得所有property的信息
PropertyInfo[] properties = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
//构造初始的query
IQueryable<TEntity> query = this.GetTable<TEntity>().AsQueryable<TEntity>();
//遍历每个property
foreach (PropertyInfo p in properties)
{
if (p != null)
{
Type t = p.PropertyType;
//加入object,Binary,和XDocument, 支持sql_variant,imager 和xml等的影射。
if (t.IsValueType || t == typeof(string) || t == typeof(System.Byte[])
|| t == typeof(object) || t == typeof(System.Xml.Linq.XDocument)
|| t == typeof(System.Data.Linq.Binary))
{
//如果不为null才算做条件
if (p.GetValue(obj, null) != null)
{
if (((ColumnAttribute)(p.GetCustomAttributes(typeof(ColumnAttribute), true)[0])).IsPrimaryKey && Convert.ToInt32(p.GetValue(obj, null)) == 0)
{
}
else
{
//构造linq 表达式
ParameterExpression param = Expression.Parameter(typeof(TEntity),"c");
Expression right = Expression.Constant(p.GetValue(obj, null));
Expression left = Expression.Property(param, p.Name);
Expression filter = Expression.Equal(left, right);
Expression<Func<TEntity, bool>> pred = Expression.Lambda<Func<TEntity, bool>>(filter, param);
query = query.Where(pred);
}
}
}
}
}
return query;
}
通过类的主键查询:
public TEntity FindKey<TEntity>(object value) where TEntity : class
{
//获得所有property的信息
PropertyInfo[] properties = typeof(TEntity).GetProperties(BindingFlags.Public | BindingFlags.Instance);
//构造初始的query
IQueryable<TEntity> query = this.GetTable<TEntity>().AsQueryable<TEntity>();
//遍历每个property
foreach (PropertyInfo p in properties)
{
if (p != null)
{
Type t = p.PropertyType;
//加入object,Binary,和XDocument, 支持sql_variant,imager 和xml等的影射。
if (t.IsValueType || t == typeof(string) || t == typeof(System.Byte[])
|| t == typeof(object) || t == typeof(System.Xml.Linq.XDocument)
|| t == typeof(System.Data.Linq.Binary))
{
//如果不为null才算做条件
if (((ColumnAttribute)(p.GetCustomAttributes(typeof(ColumnAttribute), true)[0])).IsPrimaryKey)
{
ParameterExpression param = Expression.Parameter(typeof(TEntity), "d");
Expression right = Expression.Constant(value);
Expression left = Expression.Property(param, p.Name);
Expression filter = Expression.Equal(left, right);
Expression<Func<TEntity, bool>> pred = Expression.Lambda<Func<TEntity, bool>>(filter, param);
query = query.Where(pred);
break;
}
}
}
}
return query.First();
}
}
通过实体类更新:
public void Update<TEntity>(TEntity obj) where TEntity : class
{
string str = "update [" + typeof(TEntity).Name + "] set ";
string cols = "";
string where="";
//获得所有property的信息
PropertyInfo[] properties = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
//构造初始的query
IQueryable<TEntity> query = this.GetTable<TEntity>().AsQueryable<TEntity>();
//遍历每个property
foreach (PropertyInfo p in properties)
{
if (p != null)
{
Type t = p.PropertyType;
//加入object,Binary,和XDocument, 支持sql_variant,imager 和xml等的影射。
if (t.IsValueType || t == typeof(string) || t == typeof(System.Byte[])
|| t == typeof(object) || t == typeof(System.Xml.Linq.XDocument)
|| t == typeof(System.Data.Linq.Binary))
{
//如果不为null才算做条件
if (p.GetValue(obj, null) != null)
{
if (((ColumnAttribute)(p.GetCustomAttributes(typeof(ColumnAttribute), true)[0])).IsPrimaryKey)
{
where +=" where ["+p.Name+"]="+p.GetValue(obj,null);
}
else
{
if (!(t.ToString().ToLower().Contains("string") || t.ToString().ToLower().Contains("datetime")))
cols += "["+p.Name + "]=" + p.GetValue(obj, null) + ",";
else
cols += "["+p.Name + "]='" + p.GetValue(obj, null) + "',";
}
}
}
}
}
str += cols.Substring(0,cols.Length-1) +where;
HttpContext.Current.Response.Write("<br>"+str+"<br>");
this.ExecuteCommand(str);
}
public void UpdateAll<TEntity>(IEnumerable<TEntity> obj) where TEntity : class
{
foreach (var item in obj)
{
this.Update<TEntity>(item);
}
}
更新、删除、添加代码类似,也可以实现多条数据的数据操作。代码和更新类似。代码下载地址http://fccms.googlecode.com/files/linqtoaccess.rar 里面得db.cs文件里。
通过实体类插叙db.find<T>(T t):
public IQueryable<TEntity> Find<TEntity>(TEntity obj) where TEntity : class
{
//获得所有property的信息
PropertyInfo[] properties = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
//构造初始的query
IQueryable<TEntity> query = this.GetTable<TEntity>().AsQueryable<TEntity>();
//遍历每个property
foreach (PropertyInfo p in properties)
{
if (p != null)
{
Type t = p.PropertyType;
//加入object,Binary,和XDocument, 支持sql_variant,imager 和xml等的影射。
if (t.IsValueType || t == typeof(string) || t == typeof(System.Byte[])
|| t == typeof(object) || t == typeof(System.Xml.Linq.XDocument)
|| t == typeof(System.Data.Linq.Binary))
{
//如果不为null才算做条件
if (p.GetValue(obj, null) != null)
{
if (((ColumnAttribute)(p.GetCustomAttributes(typeof(ColumnAttribute), true)[0])).IsPrimaryKey && Convert.ToInt32(p.GetValue(obj, null)) == 0)
{
}
else
{
//构造linq 表达式
ParameterExpression param = Expression.Parameter(typeof(TEntity),"c");
Expression right = Expression.Constant(p.GetValue(obj, null));
Expression left = Expression.Property(param, p.Name);
Expression filter = Expression.Equal(left, right);
Expression<Func<TEntity, bool>> pred = Expression.Lambda<Func<TEntity, bool>>(filter, param);
query = query.Where(pred);
}
}
}
}
}
return query;
}
通过类的主键查询:
public TEntity FindKey<TEntity>(object value) where TEntity : class
{
//获得所有property的信息
PropertyInfo[] properties = typeof(TEntity).GetProperties(BindingFlags.Public | BindingFlags.Instance);
//构造初始的query
IQueryable<TEntity> query = this.GetTable<TEntity>().AsQueryable<TEntity>();
//遍历每个property
foreach (PropertyInfo p in properties)
{
if (p != null)
{
Type t = p.PropertyType;
//加入object,Binary,和XDocument, 支持sql_variant,imager 和xml等的影射。
if (t.IsValueType || t == typeof(string) || t == typeof(System.Byte[])
|| t == typeof(object) || t == typeof(System.Xml.Linq.XDocument)
|| t == typeof(System.Data.Linq.Binary))
{
//如果不为null才算做条件
if (((ColumnAttribute)(p.GetCustomAttributes(typeof(ColumnAttribute), true)[0])).IsPrimaryKey)
{
ParameterExpression param = Expression.Parameter(typeof(TEntity), "d");
Expression right = Expression.Constant(value);
Expression left = Expression.Property(param, p.Name);
Expression filter = Expression.Equal(left, right);
Expression<Func<TEntity, bool>> pred = Expression.Lambda<Func<TEntity, bool>>(filter, param);
query = query.Where(pred);
break;
}
}
}
}
return query.First();
}
}
通过实体类更新:
public void Update<TEntity>(TEntity obj) where TEntity : class
{
string str = "update [" + typeof(TEntity).Name + "] set ";
string cols = "";
string where="";
//获得所有property的信息
PropertyInfo[] properties = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
//构造初始的query
IQueryable<TEntity> query = this.GetTable<TEntity>().AsQueryable<TEntity>();
//遍历每个property
foreach (PropertyInfo p in properties)
{
if (p != null)
{
Type t = p.PropertyType;
//加入object,Binary,和XDocument, 支持sql_variant,imager 和xml等的影射。
if (t.IsValueType || t == typeof(string) || t == typeof(System.Byte[])
|| t == typeof(object) || t == typeof(System.Xml.Linq.XDocument)
|| t == typeof(System.Data.Linq.Binary))
{
//如果不为null才算做条件
if (p.GetValue(obj, null) != null)
{
if (((ColumnAttribute)(p.GetCustomAttributes(typeof(ColumnAttribute), true)[0])).IsPrimaryKey)
{
where +=" where ["+p.Name+"]="+p.GetValue(obj,null);
}
else
{
if (!(t.ToString().ToLower().Contains("string") || t.ToString().ToLower().Contains("datetime")))
cols += "["+p.Name + "]=" + p.GetValue(obj, null) + ",";
else
cols += "["+p.Name + "]='" + p.GetValue(obj, null) + "',";
}
}
}
}
}
str += cols.Substring(0,cols.Length-1) +where;
HttpContext.Current.Response.Write("<br>"+str+"<br>");
this.ExecuteCommand(str);
}
public void UpdateAll<TEntity>(IEnumerable<TEntity> obj) where TEntity : class
{
foreach (var item in obj)
{
this.Update<TEntity>(item);
}
}
更新、删除、添加代码类似,也可以实现多条数据的数据操作。代码和更新类似。代码下载地址http://fccms.googlecode.com/files/linqtoaccess.rar 里面得db.cs文件里。
相关文章推荐
- linq to sql 扩展方法
- 使用扩展方法对Linq to SQl Classes扩展,使其在增删改时返回结果
- 扩展方法解决LinqToSql Contains超过2100行报错问题
- asp.net Linq TO Sql 分页方法
- 在LINQ to SQL中使用Translate方法以及修改查询用SQL 推荐
- LINQ To SQL && Lambda 使用方法小结 (转)
- 在LINQ to SQL中使用Translate方法以及修改查询用SQL
- LINQ to SQL 中 Concat、Union、Intersect、Except 方法的使用
- LINQ体验(11)——LINQ to SQL语句之Null语义和String/DateTime方法
- 解决 Linq To SQL 中 DataContext 没有 CreateTable 方法的郁闷
- Linq To Sql常用方法使用总结
- Linq To SQL 批量更新方法汇总
- LINQ to SQL - Update 方法
- 练习扩展Linq to sql 生成的 datacontext 类
- 在LINQ-TO-SQL中实现“级联删除”的方法
- LINQ体验(11)——LINQ to SQL语句之Null语义和String/DateTime方法
- 使用Linq to sql 和Linq to entity 时候写的拓展方法
- Linq To Sql常用方法使用总结【转】
- Linq To Sql常用方法使用总结
- Linq to sql 语法方法示例