【问题标题】:The type arguments cannot be inferred from the usage. Try specifying the type arguments explicitly. Candidates are:无法从用法中推断出类型参数。尝试明确指定类型参数。候选人是:
【发布时间】:2020-02-25 14:31:10
【问题描述】:

我正在尝试使用FindOneAndUpdate

我有一个简单的模型:

public class CRs
    {
        [BsonId]
        [BsonRepresentation(BsonType.ObjectId)]
        public string Id { get; set; }

        public string documentRef { get; set; }

        // fields and properties
        [BsonExtraElements]
        public BsonDocument CatchAll { get; set; }
    }

这是我的 Put 请求。

        [HttpPut("{documentRef}")]
        public CRs Update(string documentRef, [FromBody] UpdateRequest path)
        {
            var data = this.GetCollection().Find(_ => _.documentRef == documentRef).Limit(1).ToList();

            if (data.Count < 1)
            {
                Response.StatusCode = 400;
                return null;
            }

            var filter = new BsonDocument("documentRef", documentRef);

            var update = Builders<BsonDocument>.Update
                .Set(path.Path, path.Value);

            var db = Startup.MongoClientHandle.GetDatabase("ioc"); // Get singleton connection.
            var col = db.GetCollection<CRs>("inputs");

            var doc = col.FindOneAndUpdate(filter, update);

            Response.StatusCode = 200;
            return doc;
        }

但是我得到编译器错误:

  DocumentController.cs(64, 27): [CS0411] The type arguments for method 'IMongoCollection<CRs>.FindOneAndUpdate<TProjection>(FilterDefinition<CRs>, UpdateDefinition<CRs>, FindOneAndUpdateOptions<CRs, TProjection>, CancellationToken)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

我错过了什么,我几乎完全按照 Mongo 的例子。谢谢。

编辑:

我的 UpdateRequest 如下所示:

public class UpdateRequest
{
    public string Path { get; set; }
    public BsonDocument Value { get; set; }
}

【问题讨论】:

    标签: c# mongodb


    【解决方案1】:

    我使用了错误的更新/过滤器类型。应该是这样的。

            var builder = Builders<CRs>.Filter;
            var filter = builder.Eq(widget => widget.documentRef, documentRef);
    
    
            var builderUpdate = Builders<CRs>.Update;
            var update = builderUpdate.Set(path.Path, path.Value);
    
            var doc = GetCollection().FindOneAndUpdate(filter, update);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多