Hi to all.
I have a complex aggregation pipeline, where SetWindowFields is only one of the stages in the middle of it.
Since the method is defined as IAggregateFluent<BsonDocument> it stops being strongly typed and I have to work with BsonDocument in the next stages (or find a way to project it back to the type it is).
Am I missing something or is this a current limitation? I didn’t find many examples using the C# LINQ provider with SetWindowFields. All the examples have it as a final stage returning BsonDocument.
Since the function works more or less like AddFields I checked to see how this is defined but it’s not available through LINQ.
SetWindowFields is defined to return IAggregateFluent<BsonDocument> in order to reduce the number of required overloads and also to not require you to create a custom class to hold the result.
If you do want to define a custom class to hold the result of $setWindowFields you can use the existing As<TNewResult> pseudo-stage to tell the driver that you want to specify a new class to represent the documents in the pipeline at that stage.
For example, if you had the following classes:
public class C
{
public int Id { get; set; }
public int X { get; set; }
}
public class CWithAverage
{
public int Id { get; set; }
public int X { get; set; }
public double Average { get; set; }
}
You could use the CWithAverage class to represent the result of SetWindowFields like this:
// collection is of type IMongoCollection<T>
var aggregate = collection
.Aggregate()
.SetWindowFields(output: p => new { Average = p.Average(c => c.X, null) })
.As<CWithAverage>();