最近几个项目都是用的Linq to sql,发现省了不少存储过程,用起来也非常方便,分页也很好用,将一些简单的操作贴出来给大家分享一下,有不懂的同学可以问我。
Demo
实体对象
![](http://hi.csdn.net/attachment/201105/11/0_1305106205FQ6m.gif)
1.增加记录
/// <summary>
/// Linq添加对象
/// </summary>
/// <param name="missionItem">实体对象</param>
public void Insert(Corps_MissionItem missionItem)
{
MissionItemDalDataContext context = new MissionItemDalDataContext();
context.Corps_MissionItem.InsertOnSubmit(missionItem);
context.SubmitChanges();
}
/// Linq添加对象
/// </summary>
/// <param name="missionItem">实体对象</param>
public void Insert(Corps_MissionItem missionItem)
{
MissionItemDalDataContext context = new MissionItemDalDataContext();
context.Corps_MissionItem.InsertOnSubmit(missionItem);
context.SubmitChanges();
}
2.修改记录
/// <summary>
/// Linq修改对象
/// </summary>
/// <param name="id">修改的记录ID</param>
public void Update(int id)
{
MissionItemDalDataContext context = new MissionItemDalDataContext();
Corps_MissionItem missionItems = context.Corps_MissionItem.First(a => a.MissionItemId == id);
missionItems.MissionName = "修改的字段";
context.SubmitChanges();
}
/// <summary>
/// Linq修改对象
/// </summary>
/// <param name="id">修改的记录ID</param>
public void Update(int id)
{
MissionItemDalDataContext context = new MissionItemDalDataContext();
Corps_MissionItem missionItems = context.Corps_MissionItem.First(a => a.MissionItemId == id);
missionItems.MissionName = "修改的字段";
context.SubmitChanges();
}
3.删除记录
/// <summary>
/// Linq删除对象
/// </summary>
/// <param name="id">删除的记录ID</param>
public void Delete(int id)
{
MissionItemDalDataContext context = new MissionItemDalDataContext();
Corps_MissionItem missionItems = context.Corps_MissionItem.First(a => a.MissionItemId == id);
context.Corps_MissionItem.DeleteOnSubmit(missionItems);
context.SubmitChanges();
}
/// <summary>
/// Linq删除对象
/// </summary>
/// <param name="id">删除的记录ID</param>
public void Delete(int id)
{
MissionItemDalDataContext context = new MissionItemDalDataContext();
Corps_MissionItem missionItems = context.Corps_MissionItem.First(a => a.MissionItemId == id);
context.Corps_MissionItem.DeleteOnSubmit(missionItems);
context.SubmitChanges();
}
4.查询所有记录
/// <summary>
/// 用Linq查询Corps_MissionItem表的所有记录
/// </summary>
/// <returns></returns>
public List<Corps_MissionItem> GetMissionItemList()
{
MissionItemDalDataContext context = new MissionItemDalDataContext();
var list = from item in context.Corps_MissionItem select item;
return list.Count() > 0 ? list.ToList() : null;
/// <summary>
/// 用Linq查询Corps_MissionItem表的所有记录
/// </summary>
/// <returns></returns>
public List<Corps_MissionItem> GetMissionItemList()
{
MissionItemDalDataContext context = new MissionItemDalDataContext();
var list = from item in context.Corps_MissionItem select item;
return list.Count() > 0 ? list.ToList() : null;
}
5.加条件查询记录
/// <summary>
/// 加条件查询列表
/// </summary>
/// <param name="status"></param>
/// <returns></returns>
public List<Corps_MissionItem> GetMissionItemListByStatus(int status)
{
MissionItemDalDataContext context = new MissionItemDalDataContext();
var result = from item in context.Corps_MissionItem where item.Status == status select item;
return result.Count() > 0 ? result.ToList() : null;
}
/// 加条件查询列表
/// </summary>
/// <param name="status"></param>
/// <returns></returns>
public List<Corps_MissionItem> GetMissionItemListByStatus(int status)
{
MissionItemDalDataContext context = new MissionItemDalDataContext();
var result = from item in context.Corps_MissionItem where item.Status == status select item;
return result.Count() > 0 ? result.ToList() : null;
}
6.分页查询数据
/// <summary>
/// 分页查询数据
/// </summary>
/// <param name="currentPage">当前页码</param>
/// <param name="pageSize">页大小</param>
/// <param name="pageCount">总页数</param>
/// <param name="recordCount">总记录数</param>
/// <returns></returns>
public List<Corps_MissionItem> GetMissionItemListByStatus(int currentPage, int pageSize, out int pageCount, out int recordCount)
{
recordCount = 0;
pageCount = 0;
MissionItemDalDataContext context = new MissionItemDalDataContext();
var result = from item in context.Corps_MissionItem select item;
int count = result.Count();
List<Corps_MissionItem> res=null;
if (count > 0)
{
recordCount = count;
/// 分页查询数据
/// </summary>
/// <param name="currentPage">当前页码</param>
/// <param name="pageSize">页大小</param>
/// <param name="pageCount">总页数</param>
/// <param name="recordCount">总记录数</param>
/// <returns></returns>
public List<Corps_MissionItem> GetMissionItemListByStatus(int currentPage, int pageSize, out int pageCount, out int recordCount)
{
recordCount = 0;
pageCount = 0;
MissionItemDalDataContext context = new MissionItemDalDataContext();
var result = from item in context.Corps_MissionItem select item;
int count = result.Count();
List<Corps_MissionItem> res=null;
if (count > 0)
{
recordCount = count;
pageCount = count%pageSize == 0
? count/pageSize
: count/pageSize + 1;
res =result.Select(s => s).Skip((currentPage - 1)*pageSize).Take(pageSize).ToList();
? count/pageSize
: count/pageSize + 1;
res =result.Select(s => s).Skip((currentPage - 1)*pageSize).Take(pageSize).ToList();
}
return res;
}
return res;
}