Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Mongodb C#驱动程序使用谓词搜索_C#_.net_Mongodb_Mongodb .net Driver - Fatal编程技术网

如何使用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(

如何在令人敬畏的MongoDB c驱动程序上使用以下方法

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我真的没有得到你想要的。