首页 > 编程语言 >C# Linq 查询汇总

C# Linq 查询汇总

时间:2023-03-08 18:35:52浏览次数:48  
标签:C# yc 汇总 Linq danzi TIME new t1 zijian

C# Linq 查询汇总 

 

阅读目录

 


回到顶部

 分组取值、求和、计数

  

复制代码
 1   var resultlist = orderllist.GroupBy(oo => new { oo.Deptname, oo.Userid, oo.Username })
 2             .Select(ss => new OBreakfirstModel
 3             {
 4                 Number = number++,
 5                 Deptname = ss.Key.Deptname,
 6                 Userid = ss.Key.Userid,
 7                 Username = ss.Key.Username,
 8                 Ototalamount = ss.Sum(s => s.Ototalamount),
 9                 Ocount = ss.Sum(s => s.Ocount)
10             });
复制代码 回到顶部

 主从表关联,对应多条明细数据

  

复制代码
 1         var danzilist = DBconn.Query<ZuZModel>(sql_list, dynamicParameters).ToList();
 2         var zijianlist= DBconn.Query<ZiMX>(sql_jian).ToList();
 3         var resultlist = (from danzi in danzilist
 4                         select new ZuZhuangCXModel()
 5                         {
 6                             fid = danzi.fid,
 7                             rukurq=danzi.rukurq,
 8                             shiwulx=danzi.shiwulx,
 9                             wuliaobm=danzi.wuliaobm,
10                             wuliaomc=danzi.wuliaomc,
11                             wuliaolbdm=danzi.wuliaolbdm,
12                             wuliaolbmc=danzi.wuliaolbmc,
13                             shuliang=danzi.shuliang,
14                             cangkuid=danzi.cangkuid,
15                             cangkumc=danzi.cangkumc,
16                             zijianmxs = (from zijian in zijianlist
17                                         where zijian.fid == danzi.fid
18                                         select new ZiJianMX()
19                                         {
20                                             fid = zijian.fid,
21                                             fdetailid = zijian.fdetailid,
22                                             wuliaobm = zijian.wuliaobm,
23                                             wuliaomc = zijian.wuliaomc,
24                                             cangkuid = zijian.cangkuid,
25                                             cangkumc = zijian.cangkumc,
26                                             shuliang = zijian.shuliang
27                                         }).ToList()
28                         }).ToList();
复制代码 回到顶部

分组去重

    var newycmxlist = ycmxlist.GroupBy(g => new { g.EMPI, g.SHIFOUXZ })
.Select(item => item.First()).ToList();
回到顶部

两表关联、排序、对象赋值、分组去重

复制代码
           List<YiChangEMPIModel> ycmxlist = new List<YiChangEMPIModel>();
            var yichang = from yc in AnalysisDbContext.EMPI_YC
                          from ycmx in AnalysisDbContext.EMPI_YCMX
                          where yc.ID == ycmx.ZHUBIAOID && (state == "*" ? 1==1: yc.SHIFOUXZ==state)
                          && (yc.EMPI.Contains(text) || ycmx.SHENFENZH.Contains(text) || ycmx.XINGMING.Contains(text))
                          orderby yc.UPDATE_TIME descending
                          select new { 
                            ID=yc.ID,
                            EMPI=yc.EMPI,
                            SHIFOUXZ=yc.SHIFOUXZ,
                            INSERT_TIME=yc.INSERT_TIME,
                            UPDATE_TIME=yc.UPDATE_TIME
                          };
            foreach (var yc in yichang)
            {
                ycmxlist.Add(new YiChangEMPIModel
                {
                    ID = yc.ID,
                    EMPI = yc.EMPI,
                    SHIFOUXZ = yc.SHIFOUXZ,
                    INSERT_TIME = yc.INSERT_TIME,
                    UPDATE_TIME = yc.UPDATE_TIME
                });
            }
复制代码 回到顶部

两个 list 关联(inner join)

复制代码
    //部门信息 关联 日志表 取出上次处理时间
    //deptresult:包含各级部门和上传时间
    var deptresult = (from t1 in dapartments
                      join t2 in logs on t1.DeptID equals t2.DeptID
             select new DepartmentInfoModel { CurntDeptID = t1.CurntDeptID, CurntDeptName = t1.CurntDeptName, DeptID = t1.DeptID, DeptName = t1.DeptName, LeaderID = t1.LeaderID, LeaderName = t1.LeaderName, Starttime = t2.Starttime, Endtime = t2.Endtime }).ToList();
复制代码 回到顶部

两个 list 关联(left join)(list2 为空添加默认值)

复制代码
    //部门信息 关联 日志表 取出上次处理时间
    //deptresult:包含各级部门和上传时间
    var deptresult = (from t1 in dapartments
                        join t2 in logs on t1.DeptID equals t2.DeptID into temp
                        from tempresule in temp.DefaultIfEmpty(
                            new SendLogModel { Starttime = Convert.ToDateTime("2020-12-22"), Endtime = Convert.ToDateTime("2020-12-22") })
                        select new DepartmentInfoModel
                        {
                            CurntDeptID = t1.CurntDeptID,
                            CurntDeptName = t1.CurntDeptName,
                            DeptID = t1.DeptID,
                            DeptName = t1.DeptName,
                            LeaderID = t1.LeaderID,
                            LeaderName = t1.LeaderName,
                            Starttime = tempresule.Starttime,
                            Endtime = tempresule.Endtime
                        }).ToList();
复制代码 回到顶部

list.Remove 单个实例 / 多个实例

   

复制代码
        List<TemplistExpenseModel> currentlist = templistExpenseModels;
        if (userid != "" && userid != null)
        {
            currentlist.Where(ee => ee.Userid == userid).ToList().ForEach(cc => { templistExpenseModels.Remove(cc); });
            return Json(new { code = 200, msg = "清除推送列表成功!" });
    }
复制代码

  

 

标签:C#,yc,汇总,Linq,danzi,TIME,new,t1,zijian
From: https://www.cnblogs.com/sexintercourse/p/17195684.html

相关文章

  • 获取元素到body顶部的距离,offsetTop和offsetParent,getBoundingClientRect
    最近在写一个可见曝光的sdk,是当元素显示在可见区域的时候才算作曝光,并上报给服务端。思路是在元素请求回来并渲染完成之后,计算元素距离document顶部的距离offset,当页面滚动......
  • Echarts图表效果图(Make A Pie替代)
    Echarts图表效果图(MakeAPie替代)MakeAPie是由社区贡献者维护的用于ApacheECharts作品分享的第三方非官方社区。平时做Echarts图表经常需要用到,近期官方已经关闭,找到......
  • 创建docker免费个人容器镜像托管(私服)服务-阿里云
    创建容器镜像服务进入主页:容器镜像服务选择个人实例:   点击创建个人版:    创建完成,点击设置Registry登录密码: 设置完密码; 创建命名空间(全网唯一)......
  • Centos7安装Docker环境
    Dockerdaemon(Docker守护进程):运行在宿主机上,Docker守护进程,用户通过Dockerclient(Docker命令)与Dockerdaemon交互。Images(镜像):将软件环境打包好的模板,用来创建容......
  • #yyds干货盘点# LeetCode面试题:接雨水
    1.简述:给定 n个非负整数表示每个宽度为1的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。 示例1:输入:height=[0,1,0,2,1,0,1,3,2,1,2,1]输出:6解释:上面是由数......
  • 学习C语言第三弹:函数(1)
    函数是什么?数学中我们常见到函数的概念。维基百科中对函数的定义:子程序。   ·在计算机科学中,子程序(英语:Subroutine,procedure,function,routine,method,subprogram,callab......
  • 嵌入式C语言九大数据结构操作方式详解
          数据结构想必大家都不会陌生,对于一个成熟的程序员而言,熟悉和掌握数据结构和算法也是基本功之一。数据结构本身其实不过是数据按照特点关系进行存储或者组织......
  • C# 7.0 添加和增强的功能【基础篇】
    C#7.0添加和增强的功能【基础篇】 阅读目录一、out变量二、值元组(ValueTuple)三、析构元组和其他类型 四、析构函数五、模式匹配六、本地函数 七、扩展......
  • C# 6.0 添加和增强的功能【基础篇】
    C#6.0添加和增强的功能【基础篇】 阅读目录一、静态导入 二、异常筛选器 三、自动属性初始化表达式 四、Expressionbodied成员(表达式主体定义=>) 五、......
  • .Net Core redis 调用报错 '6000 Redis requests per hour' 解决 6000 此调用限制
    .NetCoreredis调用报错'6000Redisrequestsperhour'解决6000此调用限制 阅读目录问题描述仅升级动态库ServiceStsck.redis.dll报错解决('get_Db') 新......