zoukankan      html  css  js  c++  java
  • Linq 基础查询

    Linq查询语句编写工具可以用LinqPad,挺好用的

    一下是我自己学习Linq to SQL的查询语句

    //基本查询
    /*from c in WorkFlows
    select c*/

    //带条件查询
    /*from c in WorkFlows
    where c.Pid==1
    select c*/

    //查询显示不同的列
    /*from c in WorkFlows
    select new
    {
       c.ID,
       c.WorkFlowName
    }*/

    //排序
    /*from c in WorkFlows
    orderby c.ID descending
    select c*/

    //去除某个字段的重复
    /*(from c in WorkFlows
    where c.Pid == 1
    select c.Step).Distinct()*/

    //带条件的分组查询
    /*from c in WorkFlows
    where c.Pid == 1
    group c by c.Step into g
    select g*/


    /*from c in Cities
    where c.State == "北京市"
    select c*/

    //分组后查询最大的 Min或最小的
    /*from c in Cities
    group c by c.State into g
    select new
    {
       g.Key,        
       MaxId = g.Max(c => c.Id),
    }*/

    //查询包含数组中的数据
    /*
    from c in Cities
    where (new string[] { "河南省", "北京市" }).Contains(c.State)
    select c*/

    /*
    from c in Cities
    orderby c.Id descending,c.Sz_code ascending
    select c*/

    //查询Content字段包含“西”的和字段State以“河”开头的数据 并连接
    /*
    (from c in Cities
    where c.Content.Contains("西")
    select c).Union
    (from c in Cities
    where c.State.StartsWith("河")
    select c)*/

    //子查询
    /*from p in PersonTables
    select new
    {
       ID = p.ID,
       CityID = (from c in Cities where c.Id == p.CityID select c.Content),
       PersonName=p.PersonName,
       Sex = p.Sex
    }*/

    //左连接查询
    /*from p in PersonTables
    join c in Cities   //关联表
        on p.CityID equals c.Id
        into pro
        from x in pro.DefaultIfEmpty()  //显示左边没有关联的数据,如果不用DefaultIfEmpty() 则不会显示左边表的全部数据
    //from x in pro
    select new
    {
       ID = p.ID,
       PersonName = p.PersonName,
       Sex = p.Sex,
       Content = x.Content==null ? "不存在" :x.Content
    }*/

    //多表关联join查询
    /*
    from c in Cities
    join p in PersonTables  //关联第一个表
        on c.Id equals p.CityID
        into cro
        from x in cro.DefaultIfEmpty()
    join w in WorkFlows  //关联第二个表
        on x.ID equals w.ID
        into xrw
        from s in xrw.DefaultIfEmpty()
    select new
    {
       Id = c.Id,
       State = c.State,
       Content = c.Content,
       SzCode = c.Sz_code,
       Name = x.PersonName,
       Sex = x.Sex,
       WorkFlowName= s.WorkFlowName,
       Step = s.Step
    }
    */


     

  • 相关阅读:
    小素典会员订阅协议
    vue项目自动构建工具1.0,支持多页面构建
    spring boot 框架根據 sql 創建語句自動生成 MVC層類代碼
    使用 docsify 創建自己的 markdown 文檔系統
    “衣”及其部分屬字
    js原型鏈與js繼承解析
    js 中怎么获取session 值和HTML标签的属性值
    layui table动态表头 改变表格头部 重新加载表格的方法
    throw throws Throwable 关联于区别
    静态代理和动态代理的区别和联系
  • 原文地址:https://www.cnblogs.com/ajun/p/2244558.html
Copyright © 2011-2022 走看看