ImplementedByAllExpression ToIBA(Expression node, Type type) { if (node.IsNull()) { return(new ImplementedByAllExpression(type, Expression.Constant(null, typeof(string)), new TypeImplementedByAllExpression(new PrimaryKeyExpression(Expression.Constant(null, PrimaryKey.Type(typeof(TypeEntity))))), null)); } if (node is EntityExpression e) { return(new ImplementedByAllExpression(type, new SqlCastExpression(typeof(string), e.ExternalId.Value), new TypeImplementedByAllExpression(new PrimaryKeyExpression(QueryBinder.TypeConstant(e.Type))), null)); } if (node is ImplementedByExpression ib) { return(new ImplementedByAllExpression(type, new PrimaryKeyExpression(QueryBinder.Coalesce(ib.Implementations.Values.Select(a => a.ExternalId.ValueType.Nullify()).Distinct().SingleEx(), ib.Implementations.Select(e => e.Value.ExternalId))), new TypeImplementedByAllExpression(new PrimaryKeyExpression( ib.Implementations.Select(imp => new When(imp.Value.ExternalId.NotEqualsNulll(), QueryBinder.TypeConstant(imp.Key))).ToList() .ToCondition(PrimaryKey.Type(typeof(TypeEntity)).Nullify()))), null)); } if (node is ImplementedByAllExpression iba) { return(iba); } throw new UnexpectedValueException(node); }