Repository: dotnet/aspnetcore
File: src/Components/Endpoints/src/Builder/ResourcePreloadCollection.cs (C#)

Code before:
                        header.Append("; rel=").Append(property.Value);
                    }
                    else if (property.Name.Equals("preloadas", StringComparison.OrdinalIgnoreCase))
                    {
                        header.Append("; as=").Append(property.Value);
                    }
                    else if (property.Name.Equals("preloadpriority", StringComparison.OrdinalIgnoreCase))
                    {
                        header.Append("; fetchpriority=").Append(property.Value);
                    }
                    else if (property.Name.Equals("preloadcrossorigin", StringComparison.OrdinalIgnoreCase))
                    {
                        header.Append("; crossorigin=").Append(property.Value);
                    }
                    else if (property.Name.Equals("integrity", StringComparison.OrdinalIgnoreCase))
                    {
                        header.Append("; integrity=\"").Append(property.Value).Append('"');
                    }
                    else if (property.Name.Equals("preloadorder", StringComparison.OrdinalIgnoreCase))
                    {
                        if (!int.TryParse(property.Value, out order))
                        {
                            order = 0;
                        }
                    }
                }

                if (header != null)
                {
                    headers.Add((group, order, header.ToString()));
                }
            }

            foreach (var group in headers.GroupBy(h => h.Group))
            {
                _storage[group.Key ?? string.Empty] = group.OrderBy(h => h.Order).Select(h => h.Value).ToArray();
            }
        }
    }

    public bool TryGetLinkHeaders(string group, out StringValues linkHeaders)
        => _storage.TryGetValue(group, out linkHeaders);
}


Review comment (refactor):
Could we move this into its own method? `TryCreateHeader` or something like that?

Code after:
            _storage[group.Key ?? string.Empty] = group.Value.OrderBy(h => h.Order).Select(h => h.Value).ToArray();
        }
    }

    private static (int order, string header) CreateHeader(StringBuilder headerBuilder, string url, IEnumerable<ResourceAssetProperty> properties)
    {
        headerBuilder.Clear();
        headerBuilder.Append('<');
        headerBuilder.Append(url);
        headerBuilder.Append('>');

        int order = 0;
        foreach (var property in properties)
        {
            if (property.Name.Equals("preloadrel", StringComparison.OrdinalIgnoreCase))
            {
                headerBuilder.Append("; rel=").Append(property.Value);
            }
            else if (property.Name.Equals("preloadas", StringComparison.OrdinalIgnoreCase))
            {
                headerBuilder.Append("; as=").Append(property.Value);
            }
            else if (property.Name.Equals("preloadpriority", StringComparison.OrdinalIgnoreCase))
            {
                headerBuilder.Append("; fetchpriority=").Append(property.Value);
            }
            else if (property.Name.Equals("preloadcrossorigin", StringComparison.OrdinalIgnoreCase))
            {
                headerBuilder.Append("; crossorigin=").Append(property.Value);
            }
            else if (property.Name.Equals("integrity", StringComparison.OrdinalIgnoreCase))
            {
                headerBuilder.Append("; integrity=\"").Append(property.Value).Append('"');
            }
            else if (property.Name.Equals("preloadorder", StringComparison.OrdinalIgnoreCase))
            {
                if (!int.TryParse(property.Value, out order))
                {
                    order = 0;
                }
            }
        }

        return (order, headerBuilder.ToString());
    }

    public bool TryGetLinkHeaders(string group, out StringValues linkHeaders)
        => _storage.TryGetValue(group, out linkHeaders);
}