Skip to content

Cosmos: Average truncates results #26378

@ajcvickers

Description

@ajcvickers

This matches behavior in Cosmos SQL, but Smit suggested I still file this.

public class Blog
{
    public Guid Id { get; set; }
    public int Value { get; set; }
}

public class SomeDbContext : DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder
            .UseCosmos(
                "https://localhost:8081",
                "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==",
                "Daily")
            .LogTo(Console.WriteLine, LogLevel.Information)
            .EnableSensitiveDataLogging();

    public virtual DbSet<Blog> Blogs { get; set; }
}

public class Program
{
    public static void Main()
    {
        using (var context = new SomeDbContext())
        {
            context.Database.EnsureDeleted();
            context.Database.EnsureCreated();

            context.Add(new Blog { Value = 2 });
            context.Add(new Blog { Value = 3 });
            context.SaveChanges();

            Console.WriteLine($"Average value = {context.Blogs.Select(e => e.Value).Average()}.");
        }
    }
}
info: 10/16/2021 12:16:48.167 CosmosEventId.ExecutedReadNext[30102] (Microsoft.EntityFrameworkCore.Database.Command)
      Executed ReadNext (217.5037 ms, 3.1 RU) ActivityId='(null)', Container='SomeDbContext', Partition='(null)', Parameters=[]
      SELECT AVG(c["Value"]) AS c
      FROM root c
      WHERE (c["Discriminator"] = "Blog")
Average value = 2.

Metadata

Metadata

Assignees

Type

No fields configured for Bug.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions