@@ -737,6 +737,73 @@ public virtual string Literal(object?[,] values)
737737 return builder . ToString ( ) ;
738738 }
739739
740+ /// <summary>
741+ /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
742+ /// the same compatibility standards as public APIs. It may be changed or removed without notice in
743+ /// any release. You should only use it directly in your code with extreme caution and knowing that
744+ /// doing so can result in application failures when updating to a new Entity Framework Core release.
745+ /// </summary>
746+ public virtual string Literal < T > ( IList < T > values , bool vertical = false )
747+ => List ( typeof ( T ) , values , vertical ) ;
748+
749+ private string List ( Type type , IEnumerable values , bool vertical = false )
750+ {
751+ var builder = new IndentedStringBuilder ( ) ;
752+
753+ builder . Append ( "new List<" )
754+ . Append ( Reference ( type ) )
755+ . Append ( "> {" ) ;
756+
757+ if ( vertical )
758+ {
759+ builder . AppendLine ( ) ;
760+ builder . IncrementIndent ( ) ;
761+ }
762+ else
763+ {
764+ builder . Append ( " " ) ;
765+ }
766+
767+ var first = true ;
768+ foreach ( var value in values )
769+ {
770+ if ( first )
771+ {
772+ first = false ;
773+ }
774+ else
775+ {
776+ builder . Append ( "," ) ;
777+
778+ if ( vertical )
779+ {
780+ builder . AppendLine ( ) ;
781+ }
782+ else
783+ {
784+ builder . Append ( " " ) ;
785+ }
786+ }
787+
788+ builder . Append ( UnknownLiteral ( value ) ) ;
789+ }
790+
791+ if ( vertical )
792+ {
793+ builder . AppendLine ( ) ;
794+ builder . DecrementIndent ( ) ;
795+ }
796+ else
797+ {
798+ builder . Append ( " " ) ;
799+ }
800+
801+ builder . Append ( "}" ) ;
802+
803+
804+ return builder . ToString ( ) ;
805+ }
806+
740807 /// <summary>
741808 /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
742809 /// the same compatibility standards as public APIs. It may be changed or removed without notice in
@@ -844,6 +911,11 @@ public virtual string UnknownLiteral(object? value)
844911 return Array ( literalType . GetElementType ( ) ! , array ) ;
845912 }
846913
914+ if ( value is IList list && value . GetType ( ) . IsGenericType && value . GetType ( ) . GetGenericTypeDefinition ( ) == typeof ( List < > ) )
915+ {
916+ return List ( value . GetType ( ) . GetGenericArguments ( ) [ 0 ] , list ) ;
917+ }
918+
847919 var mapping = _typeMappingSource . FindMapping ( literalType ) ;
848920 if ( mapping != null )
849921 {
@@ -878,6 +950,25 @@ private bool HandleExpression(Expression expression, StringBuilder builder, bool
878950
879951 HandleList ( ( ( NewArrayExpression ) expression ) . Expressions , builder , simple : true ) ;
880952
953+ builder
954+ . Append ( " }" ) ;
955+
956+ return true ;
957+ case ExpressionType . ListInit :
958+ if ( ( ( ListInitExpression ) expression ) . Initializers . Any ( _ => _ . Arguments . Count != 1 ) )
959+ {
960+ // if there is one more than one element in the arguments we can't make a literal cleanly
961+ return false ;
962+ }
963+
964+ builder
965+ . Append ( "new " )
966+ . Append ( Reference ( expression . Type ) )
967+ . Append ( " { " ) ;
968+
969+ HandleList ( ( ( ListInitExpression ) expression )
970+ . Initializers . Select ( _ => _ . Arguments . First ( ) ) , builder , simple : true ) ;
971+
881972 builder
882973 . Append ( " }" ) ;
883974
0 commit comments