How to run complex query with Any() inside Any(). MongoDB Driver C#

若如初见. 提交于 2021-01-02 00:33:51

问题


I haven't been able to perform a complex query with an Any() inside an Any(), using MongoDB C# Driver..

I have this C# models (equivalent to the mongo database models):

[BsonCollection("alert_evaluations")]
public class AlertEvaluation : Document
{
    public ICollection<EvaluationResult> EvaluationResults { get; set; }

    public string EvaluationStatus { get; set; }

    public DateTime EvaluatedAt { get; set; }
}

public class EvaluationResult
{
    public ICollection<EvaluatedLabel> EvaluatedLabels { get; set; }

    public double ResultNumber { get; set; }
}

public class EvaluatedLabel
{
    public string LabelId { get; set; }

    public string Value { get; set; }
}

And I get a list of the next model (as a parameter to filter):

public class EvaluatedLabelFilterDTO
{
    public string LabelId { get; set; }

    public string Value { get; set; }
}

What I would need to do is to filter AlertEvaluations by getting the ones that have EvaluationResults with EvaluatedLabels that match the same LabelId and Value of the list of EvaluatedLabelFilterDTO objects.

With LinQ I tried something like this:

FilterDefinitionBuilder<AlertEvaluation>.Where(x => x.EvaluationResults.Any(e => e.EvaluatedLabels.Any(z => evaluatedLabelsToFilter.Any(f => f.LabelId == z.LabelId && f.Value == z.Value))));

But of course, this is not supported by mongo..

I'm not really familiar with mongo queries.. Is there any way to do this as a BsonDocument filter query? (Or any other solution).

(The result has to be a FilterDefinition<>, not the list already).

I could really use some help with this. Thanks!

来源:https://stackoverflow.com/questions/65480659/how-to-run-complex-query-with-any-inside-any-mongodb-driver-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!