@@ -213,7 +213,9 @@ private IDictionary<string, RelationshipObject> ConvertRelationships(IIdentifiab
213
213
214
214
foreach ( RelationshipAttribute relationship in resourceContext . Relationships )
215
215
{
216
- IncludeElementExpression includeElement = includeElements . FirstOrDefault ( element => element . Relationship . Equals ( relationship ) ) ;
216
+ IncludeElementExpression includeElement = GetFirstOrDefault ( includeElements , relationship ,
217
+ ( element , nextRelationship ) => element . Relationship . Equals ( nextRelationship ) ) ;
218
+
217
219
RelationshipObject relationshipObject = ConvertRelationship ( relationship , resource , requestKind , includeElement , includedCollection ) ;
218
220
219
221
if ( relationshipObject != null && fieldSet . Contains ( relationship ) )
@@ -231,6 +233,22 @@ private IDictionary<string, RelationshipObject> ConvertRelationships(IIdentifiab
231
233
return null ;
232
234
}
233
235
236
+ private static TSource GetFirstOrDefault < TSource , TContext > ( IEnumerable < TSource > source , TContext context , Func < TSource , TContext , bool > condition )
237
+ {
238
+ // PERF: This replacement for Enumerable.FirstOrDefault() doesn't allocate a compiler-generated closure class <>c__DisplayClass.
239
+ // https://www.jetbrains.com/help/resharper/2021.2/Fixing_Issues_Found_by_DPA.html#closures-in-lambda-expressions
240
+
241
+ foreach ( TSource item in source )
242
+ {
243
+ if ( condition ( item , context ) )
244
+ {
245
+ return item ;
246
+ }
247
+ }
248
+
249
+ return default ;
250
+ }
251
+
234
252
private RelationshipObject ConvertRelationship ( RelationshipAttribute relationship , IIdentifiable leftResource , EndpointKind requestKind ,
235
253
IncludeElementExpression includeElement , IncludedCollection includedCollection )
236
254
{
0 commit comments