简体   繁体   English

MongoDB C# .NET 驱动程序 ASP.NET Core 不支持的过滤器

[英]MongoDB C# .NET Driver ASP.NET Core Unsupported Filter

I am having trouble making my filters work with MongoDB .NET Driver, I get this error:我在使我的过滤器与 MongoDB .NET 驱动程序一起工作时遇到问题,我收到此错误:

Unsupported filter: Invoke(value(System.Func2 [Role,System.Boolean]), {document}{Model}).

When trying to run this code:尝试运行此代码时:

public virtual async Task<PartitionedModel<T>> GetByAsync(Func<T, bool> filter)
{
    Expression<Func<PartitionedModel<T>, bool>> filt = (i) => filter(i.Model);
    PartitionedModel<T> item = (await collection.FindAsync(filt)).FirstOrDefault();
    return item;
}

and the class PartitionedModel look like this:PartitionedModel如下所示:

public class PartitionedModel<T> where T : IModel
{
    public ObjectId Id { get; set; }
    public PartitionOffset PartitionOffset { get; set; }
    public T Model { get; set; }
}

I did a refacto of my code working from having collections handling IModel directly to working with PartitionedModel which is a holding class for my IModel , the GetByAsync function worked correctly before I subclassed IModel我对我的代码进行了重构,从让集合直接处理IModel到使用PartitionedModel ,它是我的IModel的持有类,在我对IModel进行子类化之前, GetByAsync函数工作正常

I found little to no informations about this problem except this: Dynamic Linq Predicate throws "Unsupported Filter" error with C# MongoDB Driver我几乎没有发现关于这个问题的信息,除了这个: Dynamic Linq Predicate throws "Unsupported Filter" error with C# MongoDB Driver

But it seems like my version of the MongoDB C# Driver doesnt accept Func<> in parameter as filter, I can only pass Builder<> or Expression<> as a filter into the Find functions但似乎我的 MongoDB C# 驱动程序版本不接受参数中的Func<>作为过滤器,我只能将Builder<>Expression<>作为过滤器传递给 Find 函数

Can someone enlighten me a bit about this error?有人可以告诉我这个错误吗?

EDIT:编辑:

I tried to run this code by replacing the FindAsync(filt) with FindAsync(_ => true) and it actually works我尝试通过将FindAsync(filt)替换为FindAsync(_ => true)来运行此代码,它确实有效

Also, here is the code that is used to retrieve the collection此外,这是用于检索集合的代码

protected readonly IMongoCollection<PartitionedModel<T>> collection;

public GenericRepository(IMongoDatabase dbContext, string collectionName)
{
    collection = dbContext.GetCollection<PartitionedModel<T>>(collectionName);
}

and the version of my driver seems to be 2.7.0我的驱动程序版本似乎是2.7.0 驱动版本

EDIT 2: I've made my query works using this:编辑 2:我已经使用这个查询了:

PartitionedModel<T> item = collection.AsQueryable().FirstOrDefault(filt);

But i'm not sure what is the implication of using a non-async version, can anyone tell me if this is wrong or if this will be a problem?但是我不确定使用非异步版本意味着什么,谁能告诉我这是错误的还是会成为问题?

Seems like current implementation of c# mongo driver is not supported delegate-based filters.似乎当前 c# mongo 驱动程序的实现不支持基于委托的过滤器。

https://github.com/mongodb/mongo-csharp-driver/blob/da0cff54c67208d979b030abb160f958d3276925/src/MongoDB.Driver/Linq/Translators/PredicateTranslator.cs#L76 https://github.com/mongodb/mongo-csharp-driver/blob/da0cff54c67208d979b030abb160f958d3276925/src/MongoDB.Driver/Linq/Translators/PredicateTranslator.cs#L76

The switch doesn't contain a ExpressionType.Invoke (the type of expression in delegate-based filter) case.该开关不包含 ExpressionType.Invoke(基于委托的过滤器中的表达式类型)案例。

Mongo doesn't work with invocation filters. Mongo 不适用于调用过滤器。 You can pass that invocation filter to LINQKit's expression expander and it will replace it with a predicate that the Mongo driver can then fully understand.您可以将该调用过滤器传递给 LINQKit 的表达式扩展器,它会将其替换为 Mongo 驱动程序随后可以完全理解的谓词。

Magic is here 魔术就在这里

See it in action here 在这里看到它的实际效果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM