@@ -47,18 +47,18 @@ join b in _scimDbContext.SCIMRepresentationAttributeLst.AsQueryable() on a.Paren
4747 . ToMongoListAsync ( ) )
4848 . Distinct ( )
4949 . ToList ( ) ;
50- total = filteredRepresentationIds . Count ( ) ;
50+ total = filteredRepresentationIds . Count ;
5151 }
5252 }
5353 else
5454 {
5555 total = await _scimDbContext . SCIMRepresentationLst . AsQueryable ( )
5656 . Where ( s => s . ResourceType == parameter . ResourceType )
57- . CountAsync ( ) ;
57+ . CountAsync ( cancellationToken ) ;
5858 }
5959
6060 if ( parameter . Count == 0 )
61- return new SearchSCIMRepresentationsResponse ( total , new List < SCIMRepresentation > ( ) ) ;
61+ return new SearchSCIMRepresentationsResponse ( total , [ ] ) ;
6262
6363 var filteredRepresentations = _scimDbContext . SCIMRepresentationLst . AsQueryable ( )
6464 . Where ( r => r . ResourceType == parameter . ResourceType ) ;
@@ -69,21 +69,20 @@ join b in _scimDbContext.SCIMRepresentationAttributeLst.AsQueryable() on a.Paren
6969 var tmp = parameter . Filter . EvaluateMongoDbRepresentations ( filteredRepresentations ) ;
7070 if ( tmp != null )
7171 {
72- var tmpIds = await tmp . Select ( r => r . Id ) . ToListAsync ( ) ;
73- if ( filteredRepresentationIds == null ) filteredRepresentationIds = tmpIds . ToList ( ) ;
72+ var tmpIds = await tmp . Select ( r => r . Id ) . ToListAsync ( cancellationToken ) ;
73+ if ( filteredRepresentationIds == null ) filteredRepresentationIds = [ .. tmpIds ] ;
7474 else
7575 {
76- var logical = parameter . Filter as SCIMLogicalExpression ;
77- if ( logical != null )
76+ if ( parameter . Filter is SCIMLogicalExpression logical )
7877 {
7978 if ( logical . LogicalOperator == Parser . Operators . SCIMLogicalOperators . AND )
80- filteredRepresentationIds = filteredRepresentationIds . Intersect ( tmpIds ) . ToList ( ) ;
79+ filteredRepresentationIds = [ .. filteredRepresentationIds . Intersect ( tmpIds ) ] ;
8180 else
82- filteredRepresentationIds = filteredRepresentationIds . Union ( tmpIds ) . ToList ( ) ;
81+ filteredRepresentationIds = [ .. filteredRepresentationIds . Union ( tmpIds ) ] ;
8382 }
8483 }
8584
86- total = filteredRepresentationIds . Count ( ) ;
85+ total = filteredRepresentationIds . Count ;
8786 }
8887 }
8988
@@ -94,15 +93,23 @@ join b in _scimDbContext.SCIMRepresentationAttributeLst.AsQueryable() on a.Paren
9493
9594 var paginationResult = await OrderByAndPaginate ( filteredRepresentations , parameter , filteredRepresentationIds ) ;
9695 filteredRepresentations = paginationResult . Query ;
97- var filteredRepresentationsWithAttributes = from a in filteredRepresentations
98- join b in _scimDbContext . SCIMRepresentationAttributeLst . AsQueryable ( ) on a . Id equals b . RepresentationId into Attributes
99- select new
100- {
101- Representation = a ,
102- Attributes = Attributes
103- } ;
104- var representationWithAttributes = await filteredRepresentationsWithAttributes
105- . ToListAsync ( ) ;
96+
97+ var representationsList = await filteredRepresentations . ToListAsync ( cancellationToken ) ;
98+ var representationIds = representationsList . Select ( r => r . Id ) . ToList ( ) ;
99+
100+ var attributes = await _scimDbContext . SCIMRepresentationAttributeLst . AsQueryable ( )
101+ . Where ( a => representationIds . Contains ( a . RepresentationId ) )
102+ . ToListAsync ( cancellationToken ) ;
103+
104+ var attributesByRepId = attributes . GroupBy ( a => a . RepresentationId )
105+ . ToDictionary ( g => g . Key , g => g . ToList ( ) ) ;
106+
107+ var representationWithAttributes = representationsList . Select ( rep => new
108+ {
109+ Representation = rep ,
110+ Attributes = attributesByRepId . ContainsKey ( rep . Id ) ? attributesByRepId [ rep . Id ] : new List < SCIMRepresentationAttribute > ( )
111+ } ) . ToList ( ) ;
112+
106113 if ( paginationResult . OrderedRepresentationIds != null )
107114 {
108115 representationWithAttributes = representationWithAttributes . Select ( r => new
@@ -119,7 +126,7 @@ join b in _scimDbContext.SCIMRepresentationAttributeLst.AsQueryable() on a.Id eq
119126 foreach ( var record in representationWithAttributes )
120127 {
121128 var representation = record . Representation ;
122- representation . FlatAttributes = record . Attributes . ToList ( ) ;
129+ representation . FlatAttributes = [ .. record . Attributes ] ;
123130 representations . Add ( representation ) ;
124131 }
125132
0 commit comments