在CosmosDb的Linq.Where()内部使用Linq.Any() [英] Use Linq.Any() inside a Linq.Where() on CosmosDb

查看:72
本文介绍了在CosmosDb的Linq.Where()内部使用Linq.Any()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将.Any()嵌套在.Where()子句中以查询本地CosmosDb模拟器.

I am trying to nest an .Any() inside a .Where() clause to query a local CosmosDb emulator.

代码如下所示;其中permittedStundentIds是变量(List<long>),a是CosmosDb中的Document

The code looks like below; where permittedStundentIds is a variable (List<long>) and a is a Document within the CosmosDb

.Where(a => permittedStudentIds.Any(sId => a.Students.Any(s => s.Id == sId)));

执行查询时,出现错误:

When I execute the query, I get the error:

不支持方法任何". ActivityId: 800000a8-0002-d600-b63f-84710c7967bb,documentdb-dotnet-sdk/1.22.0 主机/64位MicrosoftWindowsNT/10.0.16299.0

Method 'Any' is not supported. ActivityId: 800000a8-0002-d600-b63f-84710c7967bb, documentdb-dotnet-sdk/1.22.0 Host/64-bit MicrosoftWindowsNT/10.0.16299.0

我尝试了多种变体来获得等效的表达式,但无济于事.唯一有效的方法是使用.Contains()并对学生索引进行硬编码.这是不可行的,因为学生人数可能未知.

I have tried multiple variations to get an equivalent expression, but to no avail. The only one that worked was using a .Contains() and hard coding the student index; which is not feasible since the number of students may not be known.

.Where(a => permittedStudentIds.Contains(a.Students[0].Id));

我确实知道CosmosDb的Sql API尚不支持某些lambda扩展,但是对此有解决方法吗?

I do understand that certain lambda extensions are not yet supported on Sql API for CosmosDb, but is there a workaround for this?

推荐答案

尝试了各种lambda表达式的众多组合之后,这对我有用.

After trying out numerous combination of various lambda expressions, here is what worked out for me.

我在DocumentModel类中添加了StudentIds属性;多余,但仅用于过滤.

I added a StudentIds property to my DocumentModel class; redundant but used for filtering alone.

此后,我用.Contains() OR-ed查询,类似这样:

Thereafter, I OR-ed the query with .Contains(), something like this:

Expression<Func<MyDocumentModel, bool>> query = a => a.StudentIds.Contains(permittedStudentIds[0]);
foreach (var id in permittedStudentIds.Skip(1))
{
    query = query.Or(a => a.StudentIds.Contains(id));
}

,然后使用如下查询:

.Where(query);

对于query.Or()部分,我使用了以下类:

For the query.Or() part I used the following classes:

// See: https://blogs.msdn.microsoft.com/meek/2008/05/02/linq-to-entities-combining-predicates/
public static class ExpressionExtensions
{
    public static Expression<T> Compose<T>(this Expression<T> first, Expression<T> second, Func<Expression, Expression, Expression> merge)
    {
        // build parameter map (from parameters of second to parameters of first)
        var map = first.Parameters.Select((f, i) => new { f, s = second.Parameters[i] }).ToDictionary(p => p.s, p => p.f);

        // replace parameters in the second lambda expression with parameters from the first
        var secondBody = ParameterVistor.ReplaceParameters(map, second.Body);

        // apply composition of lambda expression bodies to parameters from the first expression 
        return Expression.Lambda<T>(merge(first.Body, secondBody), first.Parameters);
    }

    public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
    {
        return first.Compose(second, Expression.AndAlso);
    }

    public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
    {
        return first.Compose(second, Expression.OrElse);
    }
}


public class ParameterVistor : ExpressionVisitor
{
    private readonly Dictionary<ParameterExpression, ParameterExpression> map;

    public ParameterVistor(Dictionary<ParameterExpression, ParameterExpression> map)
    {
        this.map = map ?? new Dictionary<ParameterExpression, ParameterExpression>();
    }

    public static Expression ReplaceParameters(Dictionary<ParameterExpression, ParameterExpression> map, Expression exp)
    {
        return new ParameterVistor(map).Visit(exp);
    }

    protected override Expression VisitParameter(ParameterExpression p)
    {
        ParameterExpression replacement;
        if (map.TryGetValue(p, out replacement))
        {
            p = replacement;
        }
        return base.VisitParameter(p);
    }
}

这篇关于在CosmosDb的Linq.Where()内部使用Linq.Any()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆