子项的 Cosmos Db linq 查询不起作用

Cosmos Db linq query for child item not working

我今天在这上面花了很多时间都没有成功,我希望有人能帮助我。 我正在尝试 Cosmos DB 和 LINQ,这里是数据库中的项目:

|Customer
    |String Property1
    |String Property2
    |ICollection Orders
        |String PropertyA   <--Select on this property

如何 select 具有特定值 PropertyA 的项目客户?

我试过这个和很多其他的:

var customer = await context.Customers.Select(_s => _s.Orders.Select(p => p.PropertyA == "123456")).FirstAsync()

感谢您的帮助。

编辑 1: 我也试过这个:

        var customer1 = (from _customer in context.Customers
                         where _customer.Orders.Any(_a => _a.MyId.Contains("2012031007470165"))
                        select _customer).ToList();

这是我收到的错误消息:

The LINQ expression 'DbSet() .Where(c => EF.Property<ICollection>(c, "Orders") .AsQueryable() .Any(o => o.MyId.Contains("2012031007470165")))' 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.

如果我正确理解你的问题

var customer = await ctx.Customers.Include(c => c.Orders).FirstAsync(s => s.Orders.FirstAsync(p => p.PropertyA == "123456"));

我找到了解决方案,正确答案(与 cosmos SQL api 一起使用,不再与 Entity Framework 一起使用)是:

            Customer _customerResult = _DBcontainer.GetItemLinqQueryable<Customer>(true)
                                        .Where(_w => _w.Orders.Any(c => c.Purchase_Id == "1234"))
                                        .AsEnumerable()
                                        .FirstOrDefault();