linq to sql DAL
-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;namespace DAL
{
public class LinQBaseDao
{
/// <summary>
/// 查询所有的记录
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dc"></param>
/// <returns></returns>
public IEnumerable<T> Query<T>(DataContext dc) where T:class
{
return dc.GetTable<T>().AsEnumerable<T>() ;
}
/// <summary>
/// 按条件查询记录
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dc"></param>
/// <param name="fun"></param>
/// <returns></returns>
public IEnumerable<T> Query<T>(DataContext dc, Func<T,bool> fun) where T : class
{
return dc.GetTable<T>().Where<T>(fun).AsEnumerable<T>();
}/// <summary>
/// 查询单条 返回实体
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="fun"></param>
/// <returns></returns>
public T Single<T>(DataContext dc,Func<T, bool> fun) where T : class
{
return dc.GetTable<T>().Single<T>(fun);
}/// <summary>
/// 添加一条记录
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dc"></param>
/// <param name="tentity"></param>
public void InsertOne<T>(DataContext dc, T tentity) where T : class
{
var table = dc.GetTable<T>();
table.InsertOnSubmit(tentity);
dc.SubmitChanges();
}/// <summary>
/// 添加多条记录
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dc"></param>
/// <param name="tentitys"></param>
public void InsertToMany<T>(DataContext dc, IEnumerable<T> tentitys) where T : class
{
var table = dc.GetTable<T>();
table.InsertAllOnSubmit(tentitys);
dc.SubmitChanges();
}/// <summary>
/// 删除单条数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dc"></param>
/// <param name="tentity"></param>
/// <param name="fun"></param>
public void DeleteOne<T>(DataContext dc, T tentity, Func<T, bool> fun) where T : class
{
var table = dc.GetTable<T>();
var result = table.Where<T>(fun).First<T>();
table.DeleteOnSubmit(result);
dc.SubmitChanges();
}
/// <summary>
/// 按条件删除多条数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dc"></param>
/// <param name="tentitys"></param>
/// <param name="fun"></param>
public void DeleteToMany<T>(DataContext dc, IEnumerable<T> tentitys, Func<T, bool> fun) where T : class
{
var table = dc.GetTable<T>();
var result = table.Where<T>(fun).AsEnumerable<T>();
table.DeleteAllOnSubmit<T>(result);
dc.SubmitChanges();
}/// <summary>
/// 删除多条数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dc"></param>
/// <param name="tentitys"></param>
public void DeleteToManyByCondition<T>(DataContext dc, IEnumerable<T> tentitys) where T : class
{
var table = dc.GetTable<T>();
table.DeleteAllOnSubmit<T>(tentitys);
dc.SubmitChanges();
}/// <summary>
/// LINQ更新方法
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dc"></param>
/// <param name="fun"></param>
/// <param name="tentity"></param>
/// <param name="action"></param>
public void Update<T>(DataContext dc, Func<T, bool> fun, T tentity, Action<T> action) where T : class
{
var table = dc.GetTable<T>().Single<T>(fun);
//var table = dc.GetTable<T>().Where<T>(fun).Single<T>();
action(table);
dc.SubmitChanges();
}
}
}