score:2

Since the driver does not like Contains() or Any(). I had the build the expression dynamically using the PredicateBuilder.

public static class PredicateBuilder
{
    public static Expression<Func<T, bool>> True<T>() { return f => true; }
    public static Expression<Func<T, bool>> False<T>() { return f => false; }

    public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expr1,
                                                        Expression<Func<T, bool>> expr2)
    {
        var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
        return Expression.Lambda<Func<T, bool>>
              (Expression.OrElse(expr1.Body, invokedExpr), expr1.Parameters);
    }

    public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expr1,
                                                         Expression<Func<T, bool>> expr2)
    {
        var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
        return Expression.Lambda<Func<T, bool>>
              (Expression.AndAlso(expr1.Body, invokedExpr), expr1.Parameters);
    }
}

List ints = new List() {1,2,3};

        var predicate = PredicateBuilder.False<Forecast>();
        foreach (var i in ints)
        {
            predicate = predicate.Or(x => x.MarkdownGroupId == i);
        }

var forecastProductDay = _mdb.GetCollection(collectionName);

    var forecastProductProjections = forecastProductDay
        .AsQueryable()
        .Where(predicate.Compile())
        .SelectMany(x => x.ForecastArray, (x, fa) => new Flat        
        {
            MarkdownGroupId = x.MarkdownGroupId,
            MarkdownNumber = fa.MarkdownNumber,
            ForecastQuantity = fa.ForecastQuantity,
            ...
        })
        .GroupBy(key => new {key.MarkdownGroupId, key.MarkdownNumber})
        .Select(g => new FlatProjection
        {
            MarkdownGroupId = g.Key.MarkdownGroupId,
            MarkdownNumber = g.Key.MarkdownNumber,
            SalesQuantity = g.Sum(y => y.ForecastQuantity),
            ...
        })
        .OrderBy(x => x.MarkdownGroupId)
        .ThenBy(x => x.MarkdownNumber)
        .ToList();

Related Query

More Query from same tag