Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-1974

Count Expression in Aggregation Definition silently does the wrong thing

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 2.8.0
    • Affects Version/s: 2.4.3
    • Component/s: Linq
    • Labels:
      None
    • Minor Change

      Consider the following aggregation defintion

      Unable to find source-code formatter for language: csharp. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      DateTime now = DateTime.Now;
      Collection.Aggregate().Group(x => x.AdapterShortKey, group => new { CountFoo = group.Count(x => x.DepartureDate < now) });
      

      This code compiles and runs but does the wrong thing. Instead of counting the documents with the condition

      Unable to find source-code formatter for language: csharp. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      x.DepartureDate < now
      

      it counts all the documents.

      The workaround is to write

      Unable to find source-code formatter for language: csharp. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      group.Sum(x => x.DepartureDate < now ? 1 : 0)
      

      which does the correct thing.

            Assignee:
            dmitry.lukyanov@mongodb.com Dmitry Lukyanov (Inactive)
            Reporter:
            cypressious Kirill Rakhman
            Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: