C# 方法链接和枚举

C# 方法链接和枚举,c#,asp.net,mongodb,C#,Asp.net,Mongodb,我有来自mongodb的以下方法链 var projection = Builders<BsonDocument>.Projection.Include("x").Include("y").Exclude("_id"); var projection=Builders.projection.Include(“x”).Include(“y”).Exclude(“u id”); 我有一个变量数组,需要包含在.include属性中 枚举方法链和设置值的最佳方法是什么 提前谢谢大家,,

我有来自mongodb的以下方法链

 var projection = Builders<BsonDocument>.Projection.Include("x").Include("y").Exclude("_id");
var projection=Builders.projection.Include(“x”).Include(“y”).Exclude(“u id”);
我有一个变量数组,需要包含在.include属性中

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

提前谢谢大家,,
马蒂

试试这样:

var projection =
    new [] { "x", "y" }
        .Aggregate(
            Builders<BsonDocument>.Projection,
            (a, x) => a.Include(x))
        .Exclude("_id");
var投影=
新[]{“x”,“y”}
.合计(
建筑商,投影,
(a,x)=>a.包括(x))
.不包括(“_id”);
只要
Builders.Projection
Builders.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");
var数组=new[]{“x”,“y”};
var投影=
排列
.Skip(1)
.合计(
Builders.Projection.Include(array.First()),
(a,x)=>a.包括(x))
.不包括(“_id”);

不幸的是,它们的类型不一样,不过谢谢你。