zoukankan      html  css  js  c++  java
  • 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();
        }
    }
  • 相关阅读:
    Vue的基本使用
    django中的跨表查询梳理
    docker安装及配置
    大数据分析(一)探索性分析
    搜索引擎-一种提示词推荐算法
    shell变量详解
    机器学习之寻找KMeans的最优K
    大数据系统之监控系统(二)Flume的扩展
    大数据系统之监控系统(一)
    大数据系统之系统设计
  • 原文地址:https://www.cnblogs.com/xinchenhui/p/8035381.html
Copyright © 2011-2022 走看看