Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cosmosdb filtering against list of sub-object is not working #28016

Closed
anupdg opened this issue May 12, 2022 · 1 comment
Closed

Cosmosdb filtering against list of sub-object is not working #28016

anupdg opened this issue May 12, 2022 · 1 comment

Comments

@anupdg
Copy link

anupdg commented May 12, 2022

Comparing against a list of sub-object in a document is not working in cosmos DB. I have the following classes and db context-


public class Company {
    public string id { get; set; }
    public string name { get; set; }
    public List<StockIndex> Indexes { get; set; } = new List<StockIndex>();
}
public class StockIndex
{
    public string? Name { get; set; }
    public bool IsPrimary { get; set; } = false;
}

public class CompaniesContext : DbContext
{
    public DbSet<Company> Companies { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            => optionsBuilder.UseCosmos(
            "https://test-data.documents.azure.com:443",
            "xxxxxxxxxxxxxxxxxxxxxxxxx",
            databaseName: "dbname").
        EnableDetailedErrors().
        EnableSensitiveDataLogging();
}

Following query fails to execute-


using var companiesContext = new CompaniesContext();

try
{
    var innerDocument = new StockIndex() { Name = "DJIA" };

    var result = companiesContext.Companies.Where(c=>c.Indexes.Contains(innerDocument)).AsNoTracking();
    foreach (var c in result)
    {
        Console.WriteLine(c);
    }
}
catch (Exception ex)
{

}

Error message

The LINQ expression 'DbSet()
.Where(c => EF.Property<List>(c, "Indexes")
.AsQueryable()
.Any(p => p == __innerDocument_0))' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.

Stack traces
at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.g__CheckTranslated|15_0(ShapedQueryExpression translated, <>c__DisplayClass15_0& ) in C:\JF\efcore\src\EFCore\Query\QueryableMethodTranslatingExpressionVisitor.cs:line 108
at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression) in C:\JF\efcore\src\EFCore\Query\QueryableMethodTranslatingExpressionVisitor.cs:line 491
at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at Microsoft.EntityFrameworkCore.Cosmos.Query.Internal.CosmosQueryableMethodTranslatingExpressionVisitor.Visit(Expression expression) in C:\JF\efcore\src\EFCore.Cosmos\Query\Internal\CosmosQueryableMethodTranslatingExpressionVisitor.cs:line 140
at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression) in C:\JF\efcore\src\EFCore\Query\QueryableMethodTranslatingExpressionVisitor.cs:line 121
at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at Microsoft.EntityFrameworkCore.Cosmos.Query.Internal.CosmosQueryableMethodTranslatingExpressionVisitor.Visit(Expression expression) in C:\JF\efcore\src\EFCore.Cosmos\Query\Internal\CosmosQueryableMethodTranslatingExpressionVisitor.cs:line 140
at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query) in C:\JF\efcore\src\EFCore\Query\QueryCompilationContext.cs:line 189
at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Boolean async) in C:\JF\efcore\src\EFCore\Storage\Database.cs:line 76
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async) in C:\JF\efcore\src\EFCore\Query\Internal\QueryCompiler.cs:line 111
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass9_01.<Execute>b__0() in C:\JF\efcore\src\EFCore\Query\Internal\QueryCompiler.cs:line 95 at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func1 compiler) in C:\JF\efcore\src\EFCore\Query\Internal\CompiledQueryCache.cs:line 74
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query) in C:\JF\efcore\src\EFCore\Query\Internal\QueryCompiler.cs:line 91
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression) in C:\JF\efcore\src\EFCore\Query\Internal\EntityQueryProvider.cs:line 78
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable1.GetEnumerator() in C:\JF\efcore\src\EFCore\Query\Internal\EntityQueryable.cs:line 90
at Program.

$(String[] args) in C:\JF\POCs\POCs\New folder\CosmosEntityFrameworkPOC\Program.cs:line 33

Include provider and version information
EF Core version: 6
Database provider: Cosmos
Target framework: .net 6
Operating system: Windows 11
IDE: Visual Studio 2022

@smitpatel
Copy link
Contributor

Duplicate of #16926

@smitpatel smitpatel marked this as a duplicate of #16926 May 12, 2022
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants