• LINQ


    简单合计
    int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
    double numSum = numbers.Sum();

    合计数组中的字符数
    string[] words = { "cherry", "apple", "blueberry" };
    double totalChars = words.Sum(w => w.Length);

    找到最小的值
    int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
    int minNum = numbers.Min();

    找到字符数最少的那个串
    string[] words = { "cherry", "apple", "blueberry" };
    int shortestWord = words.Min(w => w.Length);


    检查数组中是否有至少一个包含ei的值
    string[] words = { "believe", "relief", "receipt", "field" };
    bool iAfterE = words.Any(w => w.Contains("ei"));

    取表中Name字段唯一值并且排序
    DataTable orders = dataSet.Tables[0];
    EnumerableRowCollection<DataRow> query =(from order in orders.AsEnumerable() orderby order.Field<string>("Name") select order.Field<string>("Name")).Distinct();
    DataView view = query.AsDataView();
    GridView1.DataSource=view;
    GridView1.DataBind();

    按表中Name字段分组,合计Money
    DataTable orders = dataSet.Tables[0];
    var query =from order in orders.AsEnumerable() group order by order.Field<string>("Name") into g select new { Names=g.Key,Moneys=g.Sum(order=>order.Field<decimal>("Money"))};
    GridView1.DataSource = query.AsQueryable();
    GridView1.DataBind();

    内连接
    DataTable orders = dataSet.Tables[0];
    DataTable citys = dataSet.Tables[1];
    var query =from order in orders.AsEnumerable()
               join city in citys.AsEnumerable()
               on order.Field<string>("Name") equals city.Field<string>("Name")
               select new
               {
                   Name = order.Field<string>("Name"),
                   Money = order.Field<decimal>("Money"),
                   City = city.Field<string>("City")
               };

    左连接
    DataTable orders = dataSet.Tables[0];
    DataTable citys = dataSet.Tables[1];
    var query =from order in orders.AsEnumerable()
               join city in citys.AsEnumerable()
               on order.Field<string>("Name") equals city.Field<string>("Name") into cits
               select new
               {
                   Name = order.Field<string>("Name"),
                   Money = order.Field<decimal>("Money"),
                   City = cits.Count() > 0 ? cits.First().Field<string>("City") : ""
               };

    合并并且取掉重复项
    int[] S1 = { 1, 2, 3, 4, 5 };
    int[] S2 = { 3, 2, 7, 8, 10 };

    var S3s = S1.Union(S2);
    foreach (var S3 in S3s)
      MessageBox.Show(S3.ToString());


    通过新建项LINQ TO SQL类,增加拖拽表TRANSTEST
    带事务的修改记录
    DataClassesDataContext db = new DataClassesDataContext(ConfigurationManager.ConnectionStrings["TESTDBConnectionString"].ConnectionString);
    db.CommandTimeout = 60;
    using (TransactionScope ts = new TransactionScope())
    {
      var records = db.TRANSTEST.Where(p => p.Name == "MAJ'IE");
      foreach (var record in records)
      {
        record.orders = 1;
        record.other = DateTime.Now.Second.ToString();
      }
      db.SubmitChanges();
      ts.Complete();
    }


    增加记录
    DataClassesDataContext db = new DataClassesDataContext(ConfigurationManager.ConnectionStrings["TESTDBConnectionString"].ConnectionString);
    TRANSTEST record = new TRANSTEST();
    record.other = "ILOVEYOU";
    db.TRANSTEST.InsertOnSubmit(record);
    db.SubmitChanges();

  • 相关阅读:
    [LeetCode]1290. 二进制链表转整数
    [LeetCode]1295. 统计位数为偶数的数字
    map 用法 拿到map数组每一个数据
    父子组件相互传参
    父组件给子组件传参 el-dialog 试例
    如何用JS判断div中内容为空,当为空时隐藏div
    完整的Vue+element-ui table组件实现表格内容的编辑删除和新行添加小实例
    Git操作
    charles的使用
    移动端的一些问题
  • 原文地址:https://www.cnblogs.com/e8sp/p/3049536.html
Copyright © 2020-2023  润新知