LINQ的增删改查写法&&组合查询

.ToList();//返回一个集合,包含查到的所有值;

.First();//返回查到的第一条数据,如果查不到会报错;

.FirstOrDefault();返回查到的第一条数据,差不到返回一个null;

db.Users.InsertOnSubmit();//添加

db.DeleteOrSubmit();//删除
db.SubmitChanges();//提交修改,增删改都需要提交;

//修改取到对象之后直接赋值,提交;

List<Users> ulist = db.Users.where(r => r.UserName.StartsWith("查开头关键字的方法")).ToList;

List<Users> ulist = db.Users.where(r => r.UserName.EndsWith("查结尾关键字的方法")).ToList;

List<Users> ulist = db.Users.Max(r => r.Ids).ToList;//查ids最大值的方法;

List<Users> ulist = db.Users.Min(r => r.Ids).ToList;//查ids最小值的方法;

List<Users> ulist = db.Users.Average(r => r.Ids).ToList;//查ids平均值的方法;

List<Users> ulist = db.Users.Sum(r => r.Ids).ToList;//查ids总值的方法;

List<Users> ulist = db.Users.OderBy(r => r.Ids).ToList;//升序查ids的方法;

List<Users> ulist = db.Users.OderByDescending(r => r.Ids).ToList;//升序查ids的方法;

List<Users> ulist = db.Users.Skip(i).Take(j).ToList();//跳过N条查J条数据的方法,分页查;

using (DBDataContext con = new DBDataContext())
{
//查询表内所有数据,只是查询,不会放到集合内;
    IQueryable<car> clist = con.car.AsQueryable();
//判断
if (textBox1.Text.Length > 0)
{
    clist = clist.Where(r => r.name.Contains(textBox1.Text)).AsQueryable();
}
//判断
if (textBox2.Text.Length > 0)
{
    if (comboBox1.Text == "大于等于")
    clist = clist.Where(r => r.oil >= Convert.ToDecimal(textBox2.Text)).AsQueryable();
    else if (comboBox1.Text == "小于等于")
        clist = clist.Where(r => r.oil <= Convert.ToDecimal(textBox2.Text)).AsQueryable();
    else
        clist = clist.Where(r => r.oil == Convert.ToDecimal(textBox2.Text)).AsQueryable();
}
//判断
if (textBox3.Text.Length > 0)
{
if (comboBox2.Text == "大于等于")
    clist = clist.Where(r => r.price >= Convert.ToDecimal(textBox3.Text)).AsQueryable();
else if (comboBox2.Text == "小于等于")
    clist = clist.Where(r => r.price <= Convert.ToDecimal(textBox3.Text)).AsQueryable();
else
   clist = clist.Where(r => r.price == Convert.ToDecimal(textBox3.Text)).AsQueryable();
}
//以上判断执行的代码就相当于sql语句的拼接,不会真正把数据放到集合内
//只有执行ToList();这一句代码,才会真正把数据放到集合内
List<car> llist= clist.ToList();
                

 

//基础查,把内容放到泛型集合
using(DBDataContext db = new DBDataContext())
{
   List<Users> ulist = db.Users.ToList();
}
//模糊差,contains包含
string a = "123";
using(DBDataContext db = new DBDataContext())
{
   List<Users> ulist = db.Users.where(r => r.Ids.Contains(a))ToList();
}
//精确查,
string a = "123";
using(DBDataContext db = new DBDataContext())
{
   List<Users> ulist = db.Users.where(r => r.Ids==a)ToList();
}
//添加
Users u = new Users();
u.ids = "1";
u.UserName = "2";
u.password = "3";
using(DBdataContext db = new DBDataTaxt())
{
try
{
    db.Users.InsertOnSubmit(u);
    db.SubmitChanges();
    MessageBox.Show("成功");
}
catch
{
    MessageBox.Show("失败");
}
}
//修改
string a = "1";
using(DBdataContext db = new DBDataTaxt())
{
try
{
    Users u = db.Usres.where(r => r.ids == a).FirstOrDefault();
    u.ids = "3";
    u.UserName = "2";
    u.password = "1";
    db.SubmitChanges();
    MessageBox.Show("成功");
}
catch
{
    MessageBox.Show("失败");
}
}

//删除
string a = "1";
using(DBdataContext db = new DBDataTaxt())
{
    List<Users> ulist = db.Users.where(r => r.Ids == a).ToList();
    if(ulist.Count > 0)
    {
        db.DeleteOnSubmit(ulist[0]);
        db.SubmitChanges();
    }
}

 

转载于:https://www.cnblogs.com/xinchenhui/p/8035381.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值