MongoDB C#搜索文档的方法(类型为T的文档)

MongoDB C#搜索文档的方法(类型为T的文档),c#,mongodb,generics,crud,C#,Mongodb,Generics,Crud,我确实用C为MongoDB编写了一个CRUD应用程序。我试着让它尽可能的普及(至少尽我所能的好) 因此,我使用类型T作为文档的类型 public List<T> SearchDocument<T>(string collection, string fieldName, string fieldValue) { var _collection = db.GetCollection<T>(collection);

我确实用C为MongoDB编写了一个CRUD应用程序。我试着让它尽可能的普及(至少尽我所能的好)

因此,我使用类型T作为文档的类型

        public List<T> SearchDocument<T>(string collection, string fieldName, string fieldValue)
    {
        var _collection = db.GetCollection<T>(collection);
        var filter = Builders<T>.Filter.Eq(fieldName, fieldValue);
        var result = _collection.Find(filter).ToList();
        return result;
    }
公共列表搜索文档(字符串集合、字符串字段名、字符串字段值)
{
var\u collection=db.GetCollection(collection);
var filter=Builders.filter.Eq(fieldName,fieldValue);
var result=_collection.Find(filter.ToList();
返回结果;
}
该方法工作并在MongoDb中查找具有给定字段的给定值的文档。 我不知道的是如何使“stringfiledvalue”的类型也可以说是类型t

即使我已经使用了T,如何使fieldValue的类型也通用

public List<T> SearchDocument<T>(string collection, string fieldName, string fieldValue)
公共列表搜索文档(字符串集合、字符串字段名、字符串字段值)
应该是这样的

public List<T> SearchDocument<T>(string collection, string fieldName, <an other T>  fieldValue)
公共列表搜索文档(字符串集合、字符串字段名、字段值)
第二个T和第一个T是完全不同的类型,我写在这里,希望有人能理解我的意思。我知道它不可能和SearchDoc里的一样


谢谢:)对于输入

您需要第二个泛型类型
t字段

public List<T> SearchDocument<T, TField>(string collection, string fieldName, TField fieldValue)
{
    var _collection = db.GetCollection<T>(collection);
    var filter = Builders<T>.Filter.Eq<TField>(fieldName, fieldValue);
    var result = _collection.Find(filter).ToList();
    return result;
}
公共列表搜索文档(字符串集合、字符串字段名、TField字段值)
{
var\u collection=db.GetCollection(collection);
var filter=Builders.filter.Eq(fieldName,fieldValue);
var result=_collection.Find(filter.ToList();
返回结果;
}

谢谢mickl,这是正确的,并且有效(我昨晚查看了泛型,应该自己解决;谢谢Thx的帮助)!!!