方法链接和枚举

时间:2015-07-09 03:16:46

标签: c# asp.net mongodb

我有来自mongodb的以下方法链

 var projection = Builders<BsonDocument>.Projection.Include("x").Include("y").Exclude("_id");

我有一系列变量需要包含在.Include属性中。

枚举方法链和设置值的最佳方法是什么?

提前谢谢你, 玛蒂

1 个答案:

答案 0 :(得分:2)

尝试这样的事情:

var projection =
    new [] { "x", "y" }
        .Aggregate(
            Builders<BsonDocument>.Projection,
            (a, x) => a.Include(x))
        .Exclude("_id");

只要Builders<BsonDocument>.Projection&amp; Builders<BsonDocument>.Projection.Include("x")与此应运行的类型相同。

这应该适用:

var array = new[] { "x", "y" };

var projection =
    array
        .Skip(1)
        .Aggregate(
            Builders<BsonDocument>.Projection.Include(array.First()),
            (a, x) => a.Include(x))
        .Exclude("_id");