上篇博客介绍了一下linq的基本理解,接下里介绍一下LINQ的增删改查
一、增加
dmtodoEntities db = new dmtodoEntities();
t_User user = new t_User();
user.ID = 1;
user.UserName = "千千";
user.Pwd = "qq";
db.t_User.InsertOnSubmit(user);
db.SubmitChanges();
二、删除
dmtodoEntities db = new dmtodoEntities();
t_User userDelete = (from userinfo in db.t_User where userinfo.ID == 1 select userinfo).FirstOrDefault();
if (userDelete != null)
{
db.t_User.DeleteOnSubmit(userDelete);
db.SubmitChanges();
}
三、改
public bool update(t_labe u)
{
bool b = false;
//查找出修改前的数据
t_label lb = db.t_labe.Where(a => a.UserName == u.UserName).FirstOrDefault();
if (lb != null)
{
try
{
//用修改后的值给修改前的值赋值
lb.UserName = u.UserName;
lb.PassWord = u.PassWord;
lb.NickName = u.NickName;
lb.Sex = u.Sex;
db.SubmitChanges();//执行
b = true;
}
catch { }
}
return b;
}
四、查
List<t_label> list = (from c in db.t_label where c.label_name == enlabel.label_name && c.user_id==enlabel.user_id select c).ToList();
return list;
【总结】
基本的增删改查就是这些,从数据库查询符合条件的语句,是用Lambda还是接近于SQL的语句写都可以。