如何使用Mongodb C#驱动程序使用谓词搜索
如何在令人敬畏的MongoDB c驱动程序上使用以下方法如何使用Mongodb C#驱动程序使用谓词搜索,c#,.net,mongodb,mongodb-.net-driver,C#,.net,Mongodb,Mongodb .net Driver,如何在令人敬畏的MongoDB c驱动程序上使用以下方法 public IList<TEntity>SearchFor(Expression<Func<TEntity, bool>> predicate) { return collection .AsQueryable<TEntity>() .Where(
public IList<TEntity>SearchFor(Expression<Func<TEntity, bool>> predicate)
{
return collection
.AsQueryable<TEntity>()
.Where(predicate.Compile())
.ToList();
}
public IListSearchFor(表达式谓词)
{
回收
.AsQueryable()
.Where(predicate.Compile())
.ToList();
}
例子将是理想的 只需删除
编译
,因为这会创建一个驱动程序无法转换为mongo查询的委托
:
public IList<TEntity>SearchFor(Expression<Func<TEntity, bool>> predicate)
{
return collection
.AsQueryable<TEntity>()
.Where(predicate)
.ToList();
}
public IListSearchFor(表达式谓词)
{
回收
.AsQueryable()
.Where(谓词)
.ToList();
}
但是,这确实意味着谓词表达式必须可由
MongoDB
驱动程序翻译。Ok。你能给我一个如何使用它的例子吗?@ENC0D3D这取决于你的能力,但这里:SearchFor(仓鼠=>仓鼠.名称==“Bar”)
我试过了,但它返回了0条记录。无论是否编译<代码>IList lstTickets=\u ticketRepository.SearchFor(x=>x.description==search)代码>为什么不呢?你有一只名叫bar的仓鼠吗?@ENC0D3D我真的没有得到你想要的。