C# aggregation with dynamic group stage based on params

Hello to all,

I have an aggregation pipeline where among others has a group state
{
_id: {
company: “$_id.company”
},
totalShareClicks: {
$sum: “$totalShareClicks”
},
totalShareButtonClicks: {
$sum: “$totalShareButtonClicks”
},
}

I want to transfer this to c# but I would like to have the ONLY the _id part dynamic. So, one time to use company id, other time to use offerid. The stage will return the same keys. Only the _id part will be different

Any ideas how can I achieve this?

Thanks
Yannis

Hi @Yannis_Ragkavas ,

Can you elaborate on the dynamic aspect? Is the condition based on application logic or some other field/some MQL? In the latter, you can use LINQ for the same like GroupBy(p => p.B ? p.X : p.Y) .

Thanks,

Rishit.