[{"data":1,"prerenderedAt":1498},["ShallowReactive",2],{"blog:2014:optimizing-sum-count-min-max-and-average-with-linq":3,"blogMore-Development":1324,"comments-optimizing-sum-count-min-max-and-average-with-linq":1337},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"category":11,"tags":12,"excerpt":18,"body":33,"_type":1316,"_id":1317,"_source":1318,"_file":1319,"_stem":1320,"_extension":1321,"url":1322,"wordCount":1323,"minutes":180,"commentCount":115},"/blog/2014/optimizing-sum-count-min-max-and-average-with-linq","2014",false,"en","Optimizing Sum, Count, Min, Max and Average with LINQ","LINQ is a great tool for C# programmers letting you use familiar syntax with a variety of back-end systems without having to learn another language or paradigm for many query operations.","2014-09-04T12:15:20+00:00","Development",[13,14,15,16,17],".NET","Elasticsearch","LINQ","Entity Framework","C#",{"type":19,"children":20},"root",[21,28],{"type":22,"tag":23,"props":24,"children":25},"element","p",{},[26],{"type":27,"value":9},"text",{"type":22,"tag":23,"props":29,"children":30},{},[31],{"type":27,"value":32},"Ensuring that the queries still perform well can be a bit of a chore and one set that fails quite badly are the aggregate operations when you want more than one.",{"type":19,"children":34,"toc":1310},[35,39,43,50,236,241,247,252,491,496,569,574,579,585,590,787,792,846,852,857,1061,1066,1296,1304],{"type":22,"tag":23,"props":36,"children":37},{},[38],{"type":27,"value":9},{"type":22,"tag":23,"props":40,"children":41},{},[42],{"type":27,"value":32},{"type":22,"tag":44,"props":45,"children":47},"h2",{"id":46},"multiple-sequential-queries-bad",[48],{"type":27,"value":49},"Multiple sequential queries (bad)",{"type":22,"tag":51,"props":52,"children":57},"pre",{"className":53,"code":54,"language":55,"meta":56,"style":56},"language-csharp shiki shiki-themes everforest-light dracula","var count = db.Invoices.Count();\nvar total = db.Invoices.Sum(i => i.Paid);\nvar average = db.Invoices.Average(i => i.Paid);\n","csharp","",[58],{"type":22,"tag":59,"props":60,"children":61},"code",{"__ignoreMap":56},[62,113,178],{"type":22,"tag":63,"props":64,"children":67},"span",{"class":65,"line":66},"line",1,[68,74,80,86,91,97,102,108],{"type":22,"tag":63,"props":69,"children":71},{"style":70},"--shiki-default:#3A94C5;--shiki-dark:#FF79C6",[72],{"type":27,"value":73},"var",{"type":22,"tag":63,"props":75,"children":77},{"style":76},"--shiki-default:#5C6A72;--shiki-dark:#F8F8F2",[78],{"type":27,"value":79}," count ",{"type":22,"tag":63,"props":81,"children":83},{"style":82},"--shiki-default:#F57D26;--shiki-dark:#FF79C6",[84],{"type":27,"value":85},"=",{"type":22,"tag":63,"props":87,"children":88},{"style":76},[89],{"type":27,"value":90}," db.",{"type":22,"tag":63,"props":92,"children":94},{"style":93},"--shiki-default:#35A77C;--shiki-dark:#F8F8F2",[95],{"type":27,"value":96},"Invoices",{"type":22,"tag":63,"props":98,"children":99},{"style":76},[100],{"type":27,"value":101},".",{"type":22,"tag":63,"props":103,"children":105},{"style":104},"--shiki-default:#8DA101;--shiki-dark:#50FA7B",[106],{"type":27,"value":107},"Count",{"type":22,"tag":63,"props":109,"children":110},{"style":76},[111],{"type":27,"value":112},"();\n",{"type":22,"tag":63,"props":114,"children":116},{"class":65,"line":115},2,[117,121,126,130,134,138,142,147,152,158,163,168,173],{"type":22,"tag":63,"props":118,"children":119},{"style":70},[120],{"type":27,"value":73},{"type":22,"tag":63,"props":122,"children":123},{"style":76},[124],{"type":27,"value":125}," total ",{"type":22,"tag":63,"props":127,"children":128},{"style":82},[129],{"type":27,"value":85},{"type":22,"tag":63,"props":131,"children":132},{"style":76},[133],{"type":27,"value":90},{"type":22,"tag":63,"props":135,"children":136},{"style":93},[137],{"type":27,"value":96},{"type":22,"tag":63,"props":139,"children":140},{"style":76},[141],{"type":27,"value":101},{"type":22,"tag":63,"props":143,"children":144},{"style":104},[145],{"type":27,"value":146},"Sum",{"type":22,"tag":63,"props":148,"children":149},{"style":76},[150],{"type":27,"value":151},"(",{"type":22,"tag":63,"props":153,"children":155},{"style":154},"--shiki-default:#5C6A72;--shiki-default-font-style:inherit;--shiki-dark:#FFB86C;--shiki-dark-font-style:italic",[156],{"type":27,"value":157},"i",{"type":22,"tag":63,"props":159,"children":160},{"style":82},[161],{"type":27,"value":162}," =>",{"type":22,"tag":63,"props":164,"children":165},{"style":76},[166],{"type":27,"value":167}," i.",{"type":22,"tag":63,"props":169,"children":170},{"style":93},[171],{"type":27,"value":172},"Paid",{"type":22,"tag":63,"props":174,"children":175},{"style":76},[176],{"type":27,"value":177},");\n",{"type":22,"tag":63,"props":179,"children":181},{"class":65,"line":180},3,[182,186,191,195,199,203,207,212,216,220,224,228,232],{"type":22,"tag":63,"props":183,"children":184},{"style":70},[185],{"type":27,"value":73},{"type":22,"tag":63,"props":187,"children":188},{"style":76},[189],{"type":27,"value":190}," average ",{"type":22,"tag":63,"props":192,"children":193},{"style":82},[194],{"type":27,"value":85},{"type":22,"tag":63,"props":196,"children":197},{"style":76},[198],{"type":27,"value":90},{"type":22,"tag":63,"props":200,"children":201},{"style":93},[202],{"type":27,"value":96},{"type":22,"tag":63,"props":204,"children":205},{"style":76},[206],{"type":27,"value":101},{"type":22,"tag":63,"props":208,"children":209},{"style":104},[210],{"type":27,"value":211},"Average",{"type":22,"tag":63,"props":213,"children":214},{"style":76},[215],{"type":27,"value":151},{"type":22,"tag":63,"props":217,"children":218},{"style":154},[219],{"type":27,"value":157},{"type":22,"tag":63,"props":221,"children":222},{"style":82},[223],{"type":27,"value":162},{"type":22,"tag":63,"props":225,"children":226},{"style":76},[227],{"type":27,"value":167},{"type":22,"tag":63,"props":229,"children":230},{"style":93},[231],{"type":27,"value":172},{"type":22,"tag":63,"props":233,"children":234},{"style":76},[235],{"type":27,"value":177},{"type":22,"tag":23,"props":237,"children":238},{},[239],{"type":27,"value":240},"Will issue three separate requests. There is nothing a LINQ provider can do to optimize that pattern as they are three discrete statements.",{"type":22,"tag":44,"props":242,"children":244},{"id":243},"background",[245],{"type":27,"value":246},"Background",{"type":22,"tag":23,"props":248,"children":249},{},[250],{"type":27,"value":251},"If we wanted these values by country we could do this in LINQ:",{"type":22,"tag":51,"props":253,"children":255},{"className":53,"code":254,"language":55,"meta":56,"style":56},"var a = db.Invoices.GroupBy(i => i.Country)\n      .Select(g => new { Country = g.Key,\n           Count = g.Count(),\n           Total = g.Sum(i => i.Paid),\n           Average = g.Average(i => i.Paid) });\n",[256],{"type":22,"tag":59,"props":257,"children":258},{"__ignoreMap":56},[259,318,374,399,445],{"type":22,"tag":63,"props":260,"children":261},{"class":65,"line":66},[262,266,271,275,279,283,287,292,296,300,304,308,313],{"type":22,"tag":63,"props":263,"children":264},{"style":70},[265],{"type":27,"value":73},{"type":22,"tag":63,"props":267,"children":268},{"style":76},[269],{"type":27,"value":270}," a ",{"type":22,"tag":63,"props":272,"children":273},{"style":82},[274],{"type":27,"value":85},{"type":22,"tag":63,"props":276,"children":277},{"style":76},[278],{"type":27,"value":90},{"type":22,"tag":63,"props":280,"children":281},{"style":93},[282],{"type":27,"value":96},{"type":22,"tag":63,"props":284,"children":285},{"style":76},[286],{"type":27,"value":101},{"type":22,"tag":63,"props":288,"children":289},{"style":104},[290],{"type":27,"value":291},"GroupBy",{"type":22,"tag":63,"props":293,"children":294},{"style":76},[295],{"type":27,"value":151},{"type":22,"tag":63,"props":297,"children":298},{"style":154},[299],{"type":27,"value":157},{"type":22,"tag":63,"props":301,"children":302},{"style":82},[303],{"type":27,"value":162},{"type":22,"tag":63,"props":305,"children":306},{"style":76},[307],{"type":27,"value":167},{"type":22,"tag":63,"props":309,"children":310},{"style":93},[311],{"type":27,"value":312},"Country",{"type":22,"tag":63,"props":314,"children":315},{"style":76},[316],{"type":27,"value":317},")\n",{"type":22,"tag":63,"props":319,"children":320},{"class":65,"line":115},[321,326,331,335,340,344,350,355,359,364,369],{"type":22,"tag":63,"props":322,"children":323},{"style":76},[324],{"type":27,"value":325},"      .",{"type":22,"tag":63,"props":327,"children":328},{"style":104},[329],{"type":27,"value":330},"Select",{"type":22,"tag":63,"props":332,"children":333},{"style":76},[334],{"type":27,"value":151},{"type":22,"tag":63,"props":336,"children":337},{"style":154},[338],{"type":27,"value":339},"g",{"type":22,"tag":63,"props":341,"children":342},{"style":82},[343],{"type":27,"value":162},{"type":22,"tag":63,"props":345,"children":347},{"style":346},"--shiki-default:#F85552;--shiki-dark:#FF79C6",[348],{"type":27,"value":349}," new",{"type":22,"tag":63,"props":351,"children":352},{"style":76},[353],{"type":27,"value":354}," { Country ",{"type":22,"tag":63,"props":356,"children":357},{"style":82},[358],{"type":27,"value":85},{"type":22,"tag":63,"props":360,"children":361},{"style":76},[362],{"type":27,"value":363}," g.",{"type":22,"tag":63,"props":365,"children":366},{"style":93},[367],{"type":27,"value":368},"Key",{"type":22,"tag":63,"props":370,"children":371},{"style":76},[372],{"type":27,"value":373},",\n",{"type":22,"tag":63,"props":375,"children":376},{"class":65,"line":180},[377,382,386,390,394],{"type":22,"tag":63,"props":378,"children":379},{"style":76},[380],{"type":27,"value":381},"           Count ",{"type":22,"tag":63,"props":383,"children":384},{"style":82},[385],{"type":27,"value":85},{"type":22,"tag":63,"props":387,"children":388},{"style":76},[389],{"type":27,"value":363},{"type":22,"tag":63,"props":391,"children":392},{"style":104},[393],{"type":27,"value":107},{"type":22,"tag":63,"props":395,"children":396},{"style":76},[397],{"type":27,"value":398},"(),\n",{"type":22,"tag":63,"props":400,"children":402},{"class":65,"line":401},4,[403,408,412,416,420,424,428,432,436,440],{"type":22,"tag":63,"props":404,"children":405},{"style":76},[406],{"type":27,"value":407},"           Total ",{"type":22,"tag":63,"props":409,"children":410},{"style":82},[411],{"type":27,"value":85},{"type":22,"tag":63,"props":413,"children":414},{"style":76},[415],{"type":27,"value":363},{"type":22,"tag":63,"props":417,"children":418},{"style":104},[419],{"type":27,"value":146},{"type":22,"tag":63,"props":421,"children":422},{"style":76},[423],{"type":27,"value":151},{"type":22,"tag":63,"props":425,"children":426},{"style":154},[427],{"type":27,"value":157},{"type":22,"tag":63,"props":429,"children":430},{"style":82},[431],{"type":27,"value":162},{"type":22,"tag":63,"props":433,"children":434},{"style":76},[435],{"type":27,"value":167},{"type":22,"tag":63,"props":437,"children":438},{"style":93},[439],{"type":27,"value":172},{"type":22,"tag":63,"props":441,"children":442},{"style":76},[443],{"type":27,"value":444},"),\n",{"type":22,"tag":63,"props":446,"children":448},{"class":65,"line":447},5,[449,454,458,462,466,470,474,478,482,486],{"type":22,"tag":63,"props":450,"children":451},{"style":76},[452],{"type":27,"value":453},"           Average ",{"type":22,"tag":63,"props":455,"children":456},{"style":82},[457],{"type":27,"value":85},{"type":22,"tag":63,"props":459,"children":460},{"style":76},[461],{"type":27,"value":363},{"type":22,"tag":63,"props":463,"children":464},{"style":104},[465],{"type":27,"value":211},{"type":22,"tag":63,"props":467,"children":468},{"style":76},[469],{"type":27,"value":151},{"type":22,"tag":63,"props":471,"children":472},{"style":154},[473],{"type":27,"value":157},{"type":22,"tag":63,"props":475,"children":476},{"style":82},[477],{"type":27,"value":162},{"type":22,"tag":63,"props":479,"children":480},{"style":76},[481],{"type":27,"value":167},{"type":22,"tag":63,"props":483,"children":484},{"style":93},[485],{"type":27,"value":172},{"type":22,"tag":63,"props":487,"children":488},{"style":76},[489],{"type":27,"value":490},") });\n",{"type":22,"tag":23,"props":492,"children":493},{},[494],{"type":27,"value":495},"Which gets us everything in a single statement broken down by country. In SQL this is:",{"type":22,"tag":51,"props":497,"children":501},{"className":498,"code":499,"language":500,"meta":56,"style":56},"language-sql shiki shiki-themes everforest-light dracula","SELECT Country, Count(*), Sum(Paid), Average(Paid)\n    FROM Invoices GROUP BY Country\n","sql",[502],{"type":22,"tag":59,"props":503,"children":504},{"__ignoreMap":56},[505,546],{"type":22,"tag":63,"props":506,"children":507},{"class":65,"line":66},[508,513,518,523,527,532,537,541],{"type":22,"tag":63,"props":509,"children":510},{"style":346},[511],{"type":27,"value":512},"SELECT",{"type":22,"tag":63,"props":514,"children":515},{"style":76},[516],{"type":27,"value":517}," Country, ",{"type":22,"tag":63,"props":519,"children":521},{"style":520},"--shiki-default:#DFA000;--shiki-dark:#8BE9FD",[522],{"type":27,"value":107},{"type":22,"tag":63,"props":524,"children":525},{"style":76},[526],{"type":27,"value":151},{"type":22,"tag":63,"props":528,"children":529},{"style":82},[530],{"type":27,"value":531},"*",{"type":22,"tag":63,"props":533,"children":534},{"style":76},[535],{"type":27,"value":536},"), ",{"type":22,"tag":63,"props":538,"children":539},{"style":520},[540],{"type":27,"value":146},{"type":22,"tag":63,"props":542,"children":543},{"style":76},[544],{"type":27,"value":545},"(Paid), Average(Paid)\n",{"type":22,"tag":63,"props":547,"children":548},{"class":65,"line":115},[549,554,559,564],{"type":22,"tag":63,"props":550,"children":551},{"style":346},[552],{"type":27,"value":553},"    FROM",{"type":22,"tag":63,"props":555,"children":556},{"style":76},[557],{"type":27,"value":558}," Invoices ",{"type":22,"tag":63,"props":560,"children":561},{"style":346},[562],{"type":27,"value":563},"GROUP BY",{"type":22,"tag":63,"props":565,"children":566},{"style":76},[567],{"type":27,"value":568}," Country\n",{"type":22,"tag":23,"props":570,"children":571},{},[572],{"type":27,"value":573},"Many data sources including SQL are happy to provide aggregate values without a group by so how do we generate that from LINQ?",{"type":22,"tag":23,"props":575,"children":576},{},[577],{"type":27,"value":578},"In the absence of a Group method that doesn’t take a property we need to fake it and because of the way many LINQ providers optimize out parts of the tree we can:",{"type":22,"tag":44,"props":580,"children":582},{"id":581},"single-optimized-query-good",[583],{"type":27,"value":584},"Single optimized query (good)",{"type":22,"tag":23,"props":586,"children":587},{},[588],{"type":27,"value":589},"Replacing the property in a GroupBy with a constant value gives us an optimized single query:",{"type":22,"tag":51,"props":591,"children":593},{"className":53,"code":592,"language":55,"meta":56,"style":56},"var a = db.Invoices.GroupBy(i => 1)\n    .Select(g => new { Count = g.Count(),\n               Total = g.Sum(i => i.Paid),\n               Average = g.Average(i => i.Paid) });\n",[594],{"type":22,"tag":59,"props":595,"children":596},{"__ignoreMap":56},[597,650,699,743],{"type":22,"tag":63,"props":598,"children":599},{"class":65,"line":66},[600,604,608,612,616,620,624,628,632,636,640,646],{"type":22,"tag":63,"props":601,"children":602},{"style":70},[603],{"type":27,"value":73},{"type":22,"tag":63,"props":605,"children":606},{"style":76},[607],{"type":27,"value":270},{"type":22,"tag":63,"props":609,"children":610},{"style":82},[611],{"type":27,"value":85},{"type":22,"tag":63,"props":613,"children":614},{"style":76},[615],{"type":27,"value":90},{"type":22,"tag":63,"props":617,"children":618},{"style":93},[619],{"type":27,"value":96},{"type":22,"tag":63,"props":621,"children":622},{"style":76},[623],{"type":27,"value":101},{"type":22,"tag":63,"props":625,"children":626},{"style":104},[627],{"type":27,"value":291},{"type":22,"tag":63,"props":629,"children":630},{"style":76},[631],{"type":27,"value":151},{"type":22,"tag":63,"props":633,"children":634},{"style":154},[635],{"type":27,"value":157},{"type":22,"tag":63,"props":637,"children":638},{"style":82},[639],{"type":27,"value":162},{"type":22,"tag":63,"props":641,"children":643},{"style":642},"--shiki-default:#DF69BA;--shiki-dark:#BD93F9",[644],{"type":27,"value":645}," 1",{"type":22,"tag":63,"props":647,"children":648},{"style":76},[649],{"type":27,"value":317},{"type":22,"tag":63,"props":651,"children":652},{"class":65,"line":115},[653,658,662,666,670,674,678,683,687,691,695],{"type":22,"tag":63,"props":654,"children":655},{"style":76},[656],{"type":27,"value":657},"    .",{"type":22,"tag":63,"props":659,"children":660},{"style":104},[661],{"type":27,"value":330},{"type":22,"tag":63,"props":663,"children":664},{"style":76},[665],{"type":27,"value":151},{"type":22,"tag":63,"props":667,"children":668},{"style":154},[669],{"type":27,"value":339},{"type":22,"tag":63,"props":671,"children":672},{"style":82},[673],{"type":27,"value":162},{"type":22,"tag":63,"props":675,"children":676},{"style":346},[677],{"type":27,"value":349},{"type":22,"tag":63,"props":679,"children":680},{"style":76},[681],{"type":27,"value":682}," { Count ",{"type":22,"tag":63,"props":684,"children":685},{"style":82},[686],{"type":27,"value":85},{"type":22,"tag":63,"props":688,"children":689},{"style":76},[690],{"type":27,"value":363},{"type":22,"tag":63,"props":692,"children":693},{"style":104},[694],{"type":27,"value":107},{"type":22,"tag":63,"props":696,"children":697},{"style":76},[698],{"type":27,"value":398},{"type":22,"tag":63,"props":700,"children":701},{"class":65,"line":180},[702,707,711,715,719,723,727,731,735,739],{"type":22,"tag":63,"props":703,"children":704},{"style":76},[705],{"type":27,"value":706},"               Total ",{"type":22,"tag":63,"props":708,"children":709},{"style":82},[710],{"type":27,"value":85},{"type":22,"tag":63,"props":712,"children":713},{"style":76},[714],{"type":27,"value":363},{"type":22,"tag":63,"props":716,"children":717},{"style":104},[718],{"type":27,"value":146},{"type":22,"tag":63,"props":720,"children":721},{"style":76},[722],{"type":27,"value":151},{"type":22,"tag":63,"props":724,"children":725},{"style":154},[726],{"type":27,"value":157},{"type":22,"tag":63,"props":728,"children":729},{"style":82},[730],{"type":27,"value":162},{"type":22,"tag":63,"props":732,"children":733},{"style":76},[734],{"type":27,"value":167},{"type":22,"tag":63,"props":736,"children":737},{"style":93},[738],{"type":27,"value":172},{"type":22,"tag":63,"props":740,"children":741},{"style":76},[742],{"type":27,"value":444},{"type":22,"tag":63,"props":744,"children":745},{"class":65,"line":401},[746,751,755,759,763,767,771,775,779,783],{"type":22,"tag":63,"props":747,"children":748},{"style":76},[749],{"type":27,"value":750},"               Average ",{"type":22,"tag":63,"props":752,"children":753},{"style":82},[754],{"type":27,"value":85},{"type":22,"tag":63,"props":756,"children":757},{"style":76},[758],{"type":27,"value":363},{"type":22,"tag":63,"props":760,"children":761},{"style":104},[762],{"type":27,"value":211},{"type":22,"tag":63,"props":764,"children":765},{"style":76},[766],{"type":27,"value":151},{"type":22,"tag":63,"props":768,"children":769},{"style":154},[770],{"type":27,"value":157},{"type":22,"tag":63,"props":772,"children":773},{"style":82},[774],{"type":27,"value":162},{"type":22,"tag":63,"props":776,"children":777},{"style":76},[778],{"type":27,"value":167},{"type":22,"tag":63,"props":780,"children":781},{"style":93},[782],{"type":27,"value":172},{"type":22,"tag":63,"props":784,"children":785},{"style":76},[786],{"type":27,"value":490},{"type":22,"tag":23,"props":788,"children":789},{},[790],{"type":27,"value":791},"Here are the providers I’ve tried:",{"type":22,"tag":793,"props":794,"children":795},"ul",{},[796,808,826,836],{"type":22,"tag":797,"props":798,"children":799},"li",{},[800,802],{"type":27,"value":801},"LINQ to Objects ",{"type":22,"tag":803,"props":804,"children":805},"em",{},[806],{"type":27,"value":807},"(Works although constant is likely evaluated)",{"type":22,"tag":797,"props":809,"children":810},{},[811,813],{"type":27,"value":812},"LINQ to SQL ",{"type":22,"tag":803,"props":814,"children":815},{},[816,818,824],{"type":27,"value":817},"(Works although passes ",{"type":22,"tag":819,"props":820,"children":821},"strong",{},[822],{"type":27,"value":823},"1",{"type":27,"value":825}," parameter to SQL)",{"type":22,"tag":797,"props":827,"children":828},{},[829,831],{"type":27,"value":830},"Entity Framework 6 ",{"type":22,"tag":803,"props":832,"children":833},{},[834],{"type":27,"value":835},"(Works although query is a little obscure)",{"type":22,"tag":797,"props":837,"children":838},{},[839,841],{"type":27,"value":840},"ElasticLINQ ",{"type":22,"tag":803,"props":842,"children":843},{},[844],{"type":27,"value":845},"(Works and optimizes out totally)",{"type":22,"tag":44,"props":847,"children":849},{"id":848},"countwhere-optimizations",[850],{"type":27,"value":851},"Count+Where optimizations",{"type":22,"tag":23,"props":853,"children":854},{},[855],{"type":27,"value":856},"If we are performing counts with a predicate or against a where we can also optimize these.",{"type":22,"tag":51,"props":858,"children":860},{"className":53,"code":859,"language":55,"meta":56,"style":56},"var high = db.Invoices.Count(i => i.Paid >= 1000);\nvar low = db.Invoices.Where(i => i.Paid \u003C 1000).Count();\nvar sum = db.Invoices.Sum(i => i.Paid);\n",[861],{"type":22,"tag":59,"props":862,"children":863},{"__ignoreMap":56},[864,930,1005],{"type":22,"tag":63,"props":865,"children":866},{"class":65,"line":66},[867,871,876,880,884,888,892,896,900,904,908,912,916,921,926],{"type":22,"tag":63,"props":868,"children":869},{"style":70},[870],{"type":27,"value":73},{"type":22,"tag":63,"props":872,"children":873},{"style":76},[874],{"type":27,"value":875}," high ",{"type":22,"tag":63,"props":877,"children":878},{"style":82},[879],{"type":27,"value":85},{"type":22,"tag":63,"props":881,"children":882},{"style":76},[883],{"type":27,"value":90},{"type":22,"tag":63,"props":885,"children":886},{"style":93},[887],{"type":27,"value":96},{"type":22,"tag":63,"props":889,"children":890},{"style":76},[891],{"type":27,"value":101},{"type":22,"tag":63,"props":893,"children":894},{"style":104},[895],{"type":27,"value":107},{"type":22,"tag":63,"props":897,"children":898},{"style":76},[899],{"type":27,"value":151},{"type":22,"tag":63,"props":901,"children":902},{"style":154},[903],{"type":27,"value":157},{"type":22,"tag":63,"props":905,"children":906},{"style":82},[907],{"type":27,"value":162},{"type":22,"tag":63,"props":909,"children":910},{"style":76},[911],{"type":27,"value":167},{"type":22,"tag":63,"props":913,"children":914},{"style":93},[915],{"type":27,"value":172},{"type":22,"tag":63,"props":917,"children":918},{"style":82},[919],{"type":27,"value":920}," >=",{"type":22,"tag":63,"props":922,"children":923},{"style":642},[924],{"type":27,"value":925}," 1000",{"type":22,"tag":63,"props":927,"children":928},{"style":76},[929],{"type":27,"value":177},{"type":22,"tag":63,"props":931,"children":932},{"class":65,"line":115},[933,937,942,946,950,954,958,963,967,971,975,979,983,988,992,997,1001],{"type":22,"tag":63,"props":934,"children":935},{"style":70},[936],{"type":27,"value":73},{"type":22,"tag":63,"props":938,"children":939},{"style":76},[940],{"type":27,"value":941}," low ",{"type":22,"tag":63,"props":943,"children":944},{"style":82},[945],{"type":27,"value":85},{"type":22,"tag":63,"props":947,"children":948},{"style":76},[949],{"type":27,"value":90},{"type":22,"tag":63,"props":951,"children":952},{"style":93},[953],{"type":27,"value":96},{"type":22,"tag":63,"props":955,"children":956},{"style":76},[957],{"type":27,"value":101},{"type":22,"tag":63,"props":959,"children":960},{"style":104},[961],{"type":27,"value":962},"Where",{"type":22,"tag":63,"props":964,"children":965},{"style":76},[966],{"type":27,"value":151},{"type":22,"tag":63,"props":968,"children":969},{"style":154},[970],{"type":27,"value":157},{"type":22,"tag":63,"props":972,"children":973},{"style":82},[974],{"type":27,"value":162},{"type":22,"tag":63,"props":976,"children":977},{"style":76},[978],{"type":27,"value":167},{"type":22,"tag":63,"props":980,"children":981},{"style":93},[982],{"type":27,"value":172},{"type":22,"tag":63,"props":984,"children":985},{"style":82},[986],{"type":27,"value":987}," \u003C",{"type":22,"tag":63,"props":989,"children":990},{"style":642},[991],{"type":27,"value":925},{"type":22,"tag":63,"props":993,"children":994},{"style":76},[995],{"type":27,"value":996},").",{"type":22,"tag":63,"props":998,"children":999},{"style":104},[1000],{"type":27,"value":107},{"type":22,"tag":63,"props":1002,"children":1003},{"style":76},[1004],{"type":27,"value":112},{"type":22,"tag":63,"props":1006,"children":1007},{"class":65,"line":180},[1008,1012,1017,1021,1025,1029,1033,1037,1041,1045,1049,1053,1057],{"type":22,"tag":63,"props":1009,"children":1010},{"style":70},[1011],{"type":27,"value":73},{"type":22,"tag":63,"props":1013,"children":1014},{"style":76},[1015],{"type":27,"value":1016}," sum ",{"type":22,"tag":63,"props":1018,"children":1019},{"style":82},[1020],{"type":27,"value":85},{"type":22,"tag":63,"props":1022,"children":1023},{"style":76},[1024],{"type":27,"value":90},{"type":22,"tag":63,"props":1026,"children":1027},{"style":93},[1028],{"type":27,"value":96},{"type":22,"tag":63,"props":1030,"children":1031},{"style":76},[1032],{"type":27,"value":101},{"type":22,"tag":63,"props":1034,"children":1035},{"style":104},[1036],{"type":27,"value":146},{"type":22,"tag":63,"props":1038,"children":1039},{"style":76},[1040],{"type":27,"value":151},{"type":22,"tag":63,"props":1042,"children":1043},{"style":154},[1044],{"type":27,"value":157},{"type":22,"tag":63,"props":1046,"children":1047},{"style":82},[1048],{"type":27,"value":162},{"type":22,"tag":63,"props":1050,"children":1051},{"style":76},[1052],{"type":27,"value":167},{"type":22,"tag":63,"props":1054,"children":1055},{"style":93},[1056],{"type":27,"value":172},{"type":22,"tag":63,"props":1058,"children":1059},{"style":76},[1060],{"type":27,"value":177},{"type":22,"tag":23,"props":1062,"children":1063},{},[1064],{"type":27,"value":1065},"Then we can express this as:",{"type":22,"tag":51,"props":1067,"children":1069},{"className":53,"code":1068,"language":55,"meta":56,"style":56},"var a = db.Invoices.GroupBy(g => 1)\n    .Select(g => new { High = g.Count(i => i.Paid >= 1000),\n                   Low = g.Count(i => i.Paid \u003C 1000),\n                   Sum = g.Sum(i => i.Paid) });\n",[1070],{"type":22,"tag":59,"props":1071,"children":1072},{"__ignoreMap":56},[1073,1124,1200,1252],{"type":22,"tag":63,"props":1074,"children":1075},{"class":65,"line":66},[1076,1080,1084,1088,1092,1096,1100,1104,1108,1112,1116,1120],{"type":22,"tag":63,"props":1077,"children":1078},{"style":70},[1079],{"type":27,"value":73},{"type":22,"tag":63,"props":1081,"children":1082},{"style":76},[1083],{"type":27,"value":270},{"type":22,"tag":63,"props":1085,"children":1086},{"style":82},[1087],{"type":27,"value":85},{"type":22,"tag":63,"props":1089,"children":1090},{"style":76},[1091],{"type":27,"value":90},{"type":22,"tag":63,"props":1093,"children":1094},{"style":93},[1095],{"type":27,"value":96},{"type":22,"tag":63,"props":1097,"children":1098},{"style":76},[1099],{"type":27,"value":101},{"type":22,"tag":63,"props":1101,"children":1102},{"style":104},[1103],{"type":27,"value":291},{"type":22,"tag":63,"props":1105,"children":1106},{"style":76},[1107],{"type":27,"value":151},{"type":22,"tag":63,"props":1109,"children":1110},{"style":154},[1111],{"type":27,"value":339},{"type":22,"tag":63,"props":1113,"children":1114},{"style":82},[1115],{"type":27,"value":162},{"type":22,"tag":63,"props":1117,"children":1118},{"style":642},[1119],{"type":27,"value":645},{"type":22,"tag":63,"props":1121,"children":1122},{"style":76},[1123],{"type":27,"value":317},{"type":22,"tag":63,"props":1125,"children":1126},{"class":65,"line":115},[1127,1131,1135,1139,1143,1147,1151,1156,1160,1164,1168,1172,1176,1180,1184,1188,1192,1196],{"type":22,"tag":63,"props":1128,"children":1129},{"style":76},[1130],{"type":27,"value":657},{"type":22,"tag":63,"props":1132,"children":1133},{"style":104},[1134],{"type":27,"value":330},{"type":22,"tag":63,"props":1136,"children":1137},{"style":76},[1138],{"type":27,"value":151},{"type":22,"tag":63,"props":1140,"children":1141},{"style":154},[1142],{"type":27,"value":339},{"type":22,"tag":63,"props":1144,"children":1145},{"style":82},[1146],{"type":27,"value":162},{"type":22,"tag":63,"props":1148,"children":1149},{"style":346},[1150],{"type":27,"value":349},{"type":22,"tag":63,"props":1152,"children":1153},{"style":76},[1154],{"type":27,"value":1155}," { High ",{"type":22,"tag":63,"props":1157,"children":1158},{"style":82},[1159],{"type":27,"value":85},{"type":22,"tag":63,"props":1161,"children":1162},{"style":76},[1163],{"type":27,"value":363},{"type":22,"tag":63,"props":1165,"children":1166},{"style":104},[1167],{"type":27,"value":107},{"type":22,"tag":63,"props":1169,"children":1170},{"style":76},[1171],{"type":27,"value":151},{"type":22,"tag":63,"props":1173,"children":1174},{"style":154},[1175],{"type":27,"value":157},{"type":22,"tag":63,"props":1177,"children":1178},{"style":82},[1179],{"type":27,"value":162},{"type":22,"tag":63,"props":1181,"children":1182},{"style":76},[1183],{"type":27,"value":167},{"type":22,"tag":63,"props":1185,"children":1186},{"style":93},[1187],{"type":27,"value":172},{"type":22,"tag":63,"props":1189,"children":1190},{"style":82},[1191],{"type":27,"value":920},{"type":22,"tag":63,"props":1193,"children":1194},{"style":642},[1195],{"type":27,"value":925},{"type":22,"tag":63,"props":1197,"children":1198},{"style":76},[1199],{"type":27,"value":444},{"type":22,"tag":63,"props":1201,"children":1202},{"class":65,"line":180},[1203,1208,1212,1216,1220,1224,1228,1232,1236,1240,1244,1248],{"type":22,"tag":63,"props":1204,"children":1205},{"style":76},[1206],{"type":27,"value":1207},"                   Low ",{"type":22,"tag":63,"props":1209,"children":1210},{"style":82},[1211],{"type":27,"value":85},{"type":22,"tag":63,"props":1213,"children":1214},{"style":76},[1215],{"type":27,"value":363},{"type":22,"tag":63,"props":1217,"children":1218},{"style":104},[1219],{"type":27,"value":107},{"type":22,"tag":63,"props":1221,"children":1222},{"style":76},[1223],{"type":27,"value":151},{"type":22,"tag":63,"props":1225,"children":1226},{"style":154},[1227],{"type":27,"value":157},{"type":22,"tag":63,"props":1229,"children":1230},{"style":82},[1231],{"type":27,"value":162},{"type":22,"tag":63,"props":1233,"children":1234},{"style":76},[1235],{"type":27,"value":167},{"type":22,"tag":63,"props":1237,"children":1238},{"style":93},[1239],{"type":27,"value":172},{"type":22,"tag":63,"props":1241,"children":1242},{"style":82},[1243],{"type":27,"value":987},{"type":22,"tag":63,"props":1245,"children":1246},{"style":642},[1247],{"type":27,"value":925},{"type":22,"tag":63,"props":1249,"children":1250},{"style":76},[1251],{"type":27,"value":444},{"type":22,"tag":63,"props":1253,"children":1254},{"class":65,"line":401},[1255,1260,1264,1268,1272,1276,1280,1284,1288,1292],{"type":22,"tag":63,"props":1256,"children":1257},{"style":76},[1258],{"type":27,"value":1259},"                   Sum ",{"type":22,"tag":63,"props":1261,"children":1262},{"style":82},[1263],{"type":27,"value":85},{"type":22,"tag":63,"props":1265,"children":1266},{"style":76},[1267],{"type":27,"value":363},{"type":22,"tag":63,"props":1269,"children":1270},{"style":104},[1271],{"type":27,"value":146},{"type":22,"tag":63,"props":1273,"children":1274},{"style":76},[1275],{"type":27,"value":151},{"type":22,"tag":63,"props":1277,"children":1278},{"style":154},[1279],{"type":27,"value":157},{"type":22,"tag":63,"props":1281,"children":1282},{"style":82},[1283],{"type":27,"value":162},{"type":22,"tag":63,"props":1285,"children":1286},{"style":76},[1287],{"type":27,"value":167},{"type":22,"tag":63,"props":1289,"children":1290},{"style":93},[1291],{"type":27,"value":172},{"type":22,"tag":63,"props":1293,"children":1294},{"style":76},[1295],{"type":27,"value":490},{"type":22,"tag":23,"props":1297,"children":1298},{},[1299],{"type":22,"tag":803,"props":1300,"children":1301},{},[1302],{"type":27,"value":1303},"[)amien",{"type":22,"tag":1305,"props":1306,"children":1307},"style",{},[1308],{"type":27,"value":1309},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":56,"searchDepth":115,"depth":115,"links":1311},[1312,1313,1314,1315],{"id":46,"depth":115,"text":49},{"id":243,"depth":115,"text":246},{"id":581,"depth":115,"text":584},{"id":848,"depth":115,"text":851},"markdown","content:blog:2014:optimizing-sum-count-min-max-and-average-with-linq.md","content","blog/2014/optimizing-sum-count-min-max-and-average-with-linq.md","blog/2014/optimizing-sum-count-min-max-and-average-with-linq","md","/blog/2014/optimizing-sum-count-min-max-and-average-with-linq/",540,[1325,1329,1333],{"title":1326,"date":1327,"url":1328},"HTML5 Video Cheatsheet: Optimizing videos for the web","2025-12-05T00:00:00Z","/blog/2025/html5-video-cheatsheet/",{"title":1330,"date":1331,"url":1332},"Transactions in the MongoDB EF Core Provider","2025-10-25","/blog/2025/mongodb-explicit-transactions/",{"title":1334,"date":1335,"url":1336},"Queryable Encryption with the MongoDB EF Core Provider","2025-09-22","/blog/2025/mongodb-queryable-encryption/",[1338,1360],{"_path":1339,"_dir":1340,"_draft":6,"_partial":6,"_locale":7,"title":1341,"description":1342,"id":1343,"name":1344,"email":1345,"avatar":1346,"url":1347,"date":1348,"body":1349,"_type":1316,"_id":1357,"_source":1318,"_file":1358,"_stem":1359,"_extension":1321},"/comments/optimizing-sum-count-min-max-and-average-with-linq/112832","optimizing-sum-count-min-max-and-average-with-linq","112832","LINQ to SQL does support that syntax, not sure about other providers but could be worth trying.",112832,"Damien Guard","damien@envytech.co.uk","https://www.gravatar.com/avatar/dc72963e7279d34c85ed4c0b731ce5a9?r=pg&d=retro","https://damieng.com/","2014-09-05T06:10:03",{"type":19,"children":1350,"toc":1355},[1351],{"type":22,"tag":23,"props":1352,"children":1353},{},[1354],{"type":27,"value":1342},{"title":56,"searchDepth":115,"depth":115,"links":1356},[],"content:comments:optimizing-sum-count-min-max-and-average-with-linq:112832.md","comments/optimizing-sum-count-min-max-and-average-with-linq/112832.md","comments/optimizing-sum-count-min-max-and-average-with-linq/112832",{"_path":1361,"_dir":1340,"_draft":6,"_partial":6,"_locale":7,"title":1362,"description":1363,"id":1364,"name":1365,"email":1366,"avatar":1367,"date":1368,"body":1369,"_type":1316,"_id":1495,"_source":1318,"_file":1496,"_stem":1497,"_extension":1321},"/comments/optimizing-sum-count-min-max-and-average-with-linq/112814","112814","I'm wondering if you can do",112814,"Victor Kornov","victor.kornov@gmail.com","https://www.gravatar.com/avatar/589f7928a76fbff6cf3d95267091eb0d?r=pg&d=retro","2014-09-04T22:36:30",{"type":19,"children":1370,"toc":1493},[1371,1375,1489],{"type":22,"tag":23,"props":1372,"children":1373},{},[1374],{"type":27,"value":1363},{"type":22,"tag":51,"props":1376,"children":1378},{"className":53,"code":1377,"language":55,"meta":56,"style":56},"var invoices = db.Invoices;\nvar result = invoices.Select(x => new { count = invoices.Count(), sum = ... });\n",[1379],{"type":22,"tag":59,"props":1380,"children":1381},{"__ignoreMap":56},[1382,1411],{"type":22,"tag":63,"props":1383,"children":1384},{"class":65,"line":66},[1385,1389,1394,1398,1402,1406],{"type":22,"tag":63,"props":1386,"children":1387},{"style":70},[1388],{"type":27,"value":73},{"type":22,"tag":63,"props":1390,"children":1391},{"style":76},[1392],{"type":27,"value":1393}," invoices ",{"type":22,"tag":63,"props":1395,"children":1396},{"style":82},[1397],{"type":27,"value":85},{"type":22,"tag":63,"props":1399,"children":1400},{"style":76},[1401],{"type":27,"value":90},{"type":22,"tag":63,"props":1403,"children":1404},{"style":93},[1405],{"type":27,"value":96},{"type":22,"tag":63,"props":1407,"children":1408},{"style":76},[1409],{"type":27,"value":1410},";\n",{"type":22,"tag":63,"props":1412,"children":1413},{"class":65,"line":115},[1414,1418,1423,1427,1432,1436,1440,1445,1449,1453,1458,1462,1466,1470,1475,1479,1484],{"type":22,"tag":63,"props":1415,"children":1416},{"style":70},[1417],{"type":27,"value":73},{"type":22,"tag":63,"props":1419,"children":1420},{"style":76},[1421],{"type":27,"value":1422}," result ",{"type":22,"tag":63,"props":1424,"children":1425},{"style":82},[1426],{"type":27,"value":85},{"type":22,"tag":63,"props":1428,"children":1429},{"style":76},[1430],{"type":27,"value":1431}," invoices.",{"type":22,"tag":63,"props":1433,"children":1434},{"style":104},[1435],{"type":27,"value":330},{"type":22,"tag":63,"props":1437,"children":1438},{"style":76},[1439],{"type":27,"value":151},{"type":22,"tag":63,"props":1441,"children":1442},{"style":154},[1443],{"type":27,"value":1444},"x",{"type":22,"tag":63,"props":1446,"children":1447},{"style":82},[1448],{"type":27,"value":162},{"type":22,"tag":63,"props":1450,"children":1451},{"style":346},[1452],{"type":27,"value":349},{"type":22,"tag":63,"props":1454,"children":1455},{"style":76},[1456],{"type":27,"value":1457}," { count ",{"type":22,"tag":63,"props":1459,"children":1460},{"style":82},[1461],{"type":27,"value":85},{"type":22,"tag":63,"props":1463,"children":1464},{"style":76},[1465],{"type":27,"value":1431},{"type":22,"tag":63,"props":1467,"children":1468},{"style":104},[1469],{"type":27,"value":107},{"type":22,"tag":63,"props":1471,"children":1472},{"style":76},[1473],{"type":27,"value":1474},"(), sum ",{"type":22,"tag":63,"props":1476,"children":1477},{"style":82},[1478],{"type":27,"value":85},{"type":22,"tag":63,"props":1480,"children":1481},{"style":82},[1482],{"type":27,"value":1483}," ..",{"type":22,"tag":63,"props":1485,"children":1486},{"style":76},[1487],{"type":27,"value":1488},". });\n",{"type":22,"tag":1305,"props":1490,"children":1491},{},[1492],{"type":27,"value":1309},{"title":56,"searchDepth":115,"depth":115,"links":1494},[],"content:comments:optimizing-sum-count-min-max-and-average-with-linq:112814.md","comments/optimizing-sum-count-min-max-and-average-with-linq/112814.md","comments/optimizing-sum-count-min-max-and-average-with-linq/112814",1779264582660]