private void VisitBinaryOr(BinaryExpression b) { var nodes = new BinaryExpressionDenester().GetDenestedExpressions(b, ExpressionType.Or, ExpressionType.OrElse); var values = nodes.Select(n => new BsonDocument(new BsonElementsFormatter().GetElements(n))).OfType <BsonValue>(); _elements.Add(new BsonElement("$or", new BsonArray(values))); }
private void VisitBinaryAnd(BinaryExpression b) { var nodes = new BinaryExpressionDenester().GetDenestedExpressions(b, ExpressionType.And, ExpressionType.AndAlso); var elements = nodes.SelectMany(n => new BsonElementsFormatter().GetElements(n)); var groupedElements = elements.GroupBy(x => x.Name); foreach (var grouping in groupedElements) { if (grouping.Count() == 1) { _elements.Add(grouping.Single()); } else { if (grouping.All(x => x.Value is BsonDocument)) { _elements.Add(new BsonElement(grouping.Key, new BsonDocument(grouping.SelectMany(x => ((BsonDocument)x.Value).Elements)))); } else { _elements.Add(new BsonElement("$and", new BsonArray(grouping.Select(g => new BsonDocument(g))))); } } } }