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

Linq Where method is ignored inside Collection.AsQueryable().GroupBy() of MongoDB.Driver 2.11.2

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 2.14.0
    • Affects Version/s: 2.11.2
    • Component/s: Linq
    • Labels:
      None
    • Environment:
      C# .Net

      Im writing some service in C# and I have to use MongoDB.Driver 2.11.2. I have some issue when grouping my list of items. It looks like a bug. This is my code:

      var totals = Collection.AsQueryable().GroupBy(s => s.StoreId, (key, group) => new Test
              {
                  Key = key,
      
                  LabOnHandTestCount =
                      group.Count(groupItem => groupItem.IsLab.HasValue && groupItem.IsLab.Value),
      
                  LabOnHandPrice =
                      group.Where(groupItem => groupItem.IsLab.HasValue && groupItem.IsLabd.Value)
                          .Sum(z => z.TotalCost),
      
      
                  LabOnHandPrice2 =
                      group.Sum(g=> g.IsLa.HasValue && g.IsLab.Value ? g.TotalCost:0),
      
                  LabOnHandPrice3 =
                   group.Where(gg => gg.IsLab.Value == true).Sum(s=>s.TotalCost),
      
              }).ToList();
      

      I got different results from next lines.

      • first line:
      LabOnHandPrice = group.Where(groupItem => groupItem.IsLab.HasValue && groupItem.IsLabd.Value)
                   .Sum(z => z.TotalCost)
      
      • second line(the right result):
      LabOnHandPrice2 = group.Sum(g=> g.IsLa.HasValue && g.IsLab.Value ? g.TotalCost:0),
      

      It looks like "Where" method of "first line" got ignored and calculation performed on wrong values. And also the result of the "first line" does not changes if i change a condition. I did not find any information on MongoDB official site.

       

      Why "Where" method got ignored and i have different results? Is there any solution?

      Tnx in advance.

            Assignee:
            Unassigned Unassigned
            Reporter:
            denis@r2net.com God God
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: