Rak*_*iah 6 c# mongodb mongodb-query mongodb-.net-driver asp.net-core
我在使过滤器与 MongoDB .NET 驱动程序一起使用时遇到问题,出现此错误:
Unsupported filter: Invoke(value(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;
}
Run Code Online (Sandbox Code Playgroud)
类PartitionedModel如下所示:
public class PartitionedModel<T> where T : IModel
{
public ObjectId Id { get; set; }
public PartitionOffset PartitionOffset { get; set; }
public T Model { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我对我的代码进行了重构,从让集合直接处理IModel到使用PartitionedModel这是我的IModel的持有类,GetByAsync函数在我对IModel进行子类化之前工作正常
除了这个,我几乎没有发现关于这个问题的信息: Dynamic Linq Predicate throws "Unsupported Filter" error with C# MongoDB Driver
但似乎我的 MongoDB C# 驱动程序版本不接受参数中的Func<>作为过滤器,我只能将Builder<>或Expression<>作为过滤器传递到 Find 函数中
有人可以对这个错误有所启发吗?
编辑:
我试图通过替换FindAsync(filt)with来运行这段代码FindAsync(_ => true),它确实有效
此外,这里是用于检索集合的代码
protected readonly IMongoCollection<PartitionedModel<T>> collection;
public GenericRepository(IMongoDatabase dbContext, string collectionName)
{
collection = dbContext.GetCollection<PartitionedModel<T>>(collectionName);
}
Run Code Online (Sandbox Code Playgroud)
编辑 2: 我已经使用这个使我的查询工作:
PartitionedModel<T> item = collection.AsQueryable().FirstOrDefault(filt);
但我不确定使用非异步版本的含义是什么,谁能告诉我这是错误的还是会出现问题?
| 归档时间: |
|
| 查看次数: |
2712 次 |
| 最近记录: |