MongoDB C#.NET驱动程序ASP.NET核心不支持的筛选器

MongoDB C#.NET驱动程序ASP.NET核心不支持的筛选器,c#,mongodb,asp.net-core,mongodb-query,mongodb-.net-driver,C#,Mongodb,Asp.net Core,Mongodb Query,Mongodb .net Driver,我在使用MongoDB.NET驱动程序使用过滤器时遇到问题,出现以下错误: 不支持的筛选器:调用(值(System.Func2[Role,System.Boolean]),{document}{Model})。 尝试运行此代码时: public virtual async Task<PartitionedModel<T>> GetByAsync(Func<T, bool> filter) { Expression<Func<Partitio

我在使用MongoDB.NET驱动程序使用过滤器时遇到问题,出现以下错误:

不支持的筛选器:调用(值(System.Func2[Role,System.Boolean]),{document}{Model})。

尝试运行此代码时:

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;
}
我的驱动程序的版本似乎是2.7.0

编辑2: 我使用以下方法进行了查询:

PartitionedModel item=collection.AsQueryable().FirstOrDefault(filt)


但我不确定使用非异步版本意味着什么,有人能告诉我这是错误的还是会有问题吗?

似乎当前的c#mongo驱动程序实现不支持基于委托的过滤器


开关不包含ExpressionType.Invoke(基于委托的筛选器中的表达式类型)大小写。

Mongo不适用于调用筛选器。您可以将该调用过滤器传递给LINQKit的表达式扩展器,它将用Mongo驱动程序可以完全理解的谓词替换它


您是否有更多关于用法的解释来源?无法使其工作,我在另一篇文章中看到了您的答案,但仍然是。给定任何表达式,只需在将其发送到任何Mongo函数之前调用LINQKit提供的.Expand()扩展方法。第二个链接从子文档获取选择器表达式,并通过谓词生成器将and子句链接在一起。在针对数据库和事务性文档运行的每个查询中,我都会将扩展的表达式输入Mongo。去年夏天LINQKit爆炸时,我为它写了测试。
public class PartitionedModel<T> where T : IModel
{
    public ObjectId Id { get; set; }
    public PartitionOffset PartitionOffset { get; set; }
    public T Model { get; set; }
}
protected readonly IMongoCollection<PartitionedModel<T>> collection;

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