首页 > 其他分享 >linq left join group by count组合统计,防止count()为null结果为1的错误。

linq left join group by count组合统计,防止count()为null结果为1的错误。

时间:2023-07-06 15:13:38浏览次数:37  
标签:count group carNum car linq carplan join id

原生sql

select car.id,carnum,count(carplan.carid) as timeLen from tab_car as car
join tab_inComeType as income
on car.inComeTypeId=income.Id and inComeTypeId=1 and stateCode=1 and carTypeId=1 
left join tab_carPlan as carplan
on car.id=carplan.carId and carplan.planDate='2023-7-6' and carplan.isCancel=0
group by carnum,car.id
order by car.id asc

结果:

linq 写法:

var list = from car in carList.Where(e => e.inComeTypeId == incomeTypeId && e.stateCode == (int)stateEnum.有效 && e.carTypeId == carTypeId)
                       join income in incomeList
                       on car.inComeTypeId equals income.Id
                       join carplan in carPlanList.Where(e => e.planDate == planDate && e.isCancel == 0)
                       on car.id equals carplan.carId into temp
                       from tt in temp.DefaultIfEmpty()
                       group tt by new { car.carNum, car.id } into g
                       orderby g.Key.carNum ascending
                       select new
                       {
                           carId = g.Key.id,
                           carNum = g.Key.carNum,
                           timeLen = g.Count(e => e != null) //防止为null时统计结果为1
                       };

api输出结果

人生三苦:读书、赶马、磨豆腐。每天努力进步,加油奥利给!

标签:count,group,carNum,car,linq,carplan,join,id
From: https://www.cnblogs.com/clsoft/p/17532163.html

相关文章

  • 求字典中的最大的值 max(age_counts,key=age_counts.get)
    student_info={15:{'jack','rose',},18:{'jj'},35:{'a','b','c','d'},}age_counts={}#定义一个字典key=agevalue=name的长度forageinstudent_info:count=len(student_info......
  • mysql中count(*)和found_rows()的区别
    count(*)和found_rows()都可以用来求查询记录的数量而count(*)往往单独使用,found_rows()却可以跟上前面一个查询,即select*fromtablelimit10;selectfound_rows();这样的总共可分为几个区别:1.count(*)查询的无论是否为空,都会计数,而found_rows()却不是......
  • .Net下验证MongoDB 的 Linq 模式联合查询是否可用
    MongoDB.Driver类库提供了Linq查询的支持。然而,在使用Linq进行联合查询时,是否能够正确转换为MongoDB底层的查询语句还有待验证。今天,我将进行实验来验证一下。输出查询语句首先,通过订阅MongoClientSettings的功能,将查询语句输出。varsettings=MongoCli......
  • vue 基于 CountUp.js,可用于创建显示数字数据的动画。
    地址:https://github.com/xlsdg/vue-countup-v2Installation$npminstall--savecountup.jsvue-countup-v2Usage<template><divclass="iCountUp"><ICountUp:delay="delay":endVal="endVal"......
  • Loops should be simplified with "LINQ" expressions
    Loopsshouldbesimplifiedwith"LINQ"expressionsWhyisthisanissue?Whenaloopisfiltering,selectingoraggregating,thosefunctionscanbehandledwithaclearer,moreconciseLINQexpressioninstead.Noncompliantcodeexamplevarresu......
  • Getting Started with Eclipse and Counterclockwise
    http://dev.clojure.org/display/doc/Getting+Started+with+Eclipse+and+CounterclockwiseGotostartofmetadata InstallEclipse(IfyoudonothaveEclipseinstalled,oryouwanttomakeisolatedtestsoftheClojurepluginforEclipse)GrabtheEcli......
  • 【差分 Trick】CF626F Group Projects
    模拟赛垫底哥来补题了。先排序,考虑到原来的弱智状态难以描述,我们可以这样写:\(f_{i,j,k}\)表示前\(i\)个,\(j\)段未闭合,目前的不协调值为\(k\)。然后喜提\(n^2\suma_i\)的时间复杂的。然后就是经典tricktime,这个可以看作很多线段。然后\(a_r-a_l=\suma_{i+......
  • 100W数据去重,用distinct还是group by
    京东太狠:100W数据去重,用distinct还是groupby,说说理由?原创 40岁老架构师尼恩 技术自由圈 2023-06-0411:37 发表于广东收录于合集#面试题86个技术自由圈疯狂创客圈(技术自由架构圈):一个技术狂人、技术大神、高性能发烧友圈子。圈内一大波顶级高手、架构师、......
  • 14 | count(*)这么慢,我该怎么办?
    一下内容出自《MySQL实战45讲》14|count(*)这么慢,我该怎么办?count(*)的实现方式不同的MySQL引擎中,count(*)有不同的实现方式。MyISAM引擎把一个表的总行数存在了磁盘上,执行count(*)的时候会直接返回这个数,效率很高;InnoDB引擎就执行count(*)的时候,需要把数......
  • 光脚丫学LINQ(041):使用对象关系设计器修改映射关系
    演示重点此演示视频主要介绍了如何使用VS提供的【对象关系设计器】这个工具来建立实体类之间的关系。虽然此工具可以自动根据数据表之间的关系来建立起对象模型中实体类与实体类之间的关系。然而,默认情况下,它所建立的关系貌似都是清一色的一对多关系。^_^而事实上,LINQtoSQL可以支......