Skip to content

Commit 8697d12

Browse files
committed
feat: Add FirstBestMatchForLanguage for IEnumerable<IHasLanguageCode>
1 parent fe32494 commit 8697d12

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

src/VirtoCommerce.Xapi.Core/Extensions/IHasLanguageExtensions.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@ namespace VirtoCommerce.Xapi.Core.Extensions
88
{
99
public static class IHasLanguageExtensions
1010
{
11+
/// <summary>
12+
/// Looking for first best-match language-specific value in the enumerable
13+
/// </summary>
14+
/// <param name="hasLanguages">An enumerable with values to search</param>
15+
/// <param name="language">Language to search</param>
16+
/// <returns>First matching item to the specified language</returns>
17+
public static IHasLanguageCode FirstBestMatchForLanguage(this IEnumerable<IHasLanguageCode> hasLanguages, string language)
18+
{
19+
return hasLanguages.FirstBestMatchForLanguage(x => x.LanguageCode, language);
20+
}
21+
1122
/// <summary>
1223
/// Looking for first best-match language-specific value in the enumerable, based on specified language value selector function
1324
/// </summary>
@@ -20,12 +31,14 @@ public static T FirstBestMatchForLanguage<T>(this IEnumerable<T> hasLanguages, F
2031
{
2132
ArgumentNullException.ThrowIfNull(hasLanguages);
2233

23-
//Try find object for passed language event if it null
24-
var result = hasLanguages.FirstOrDefault(x => langSelector(x)?.EqualsIgnoreCase(language) ?? langSelector(x) == language);
34+
var hasLanguagesCollection = hasLanguages as ICollection<T> ?? hasLanguages.ToList();
35+
36+
//Try to find object for passed language event if it is null
37+
var result = hasLanguagesCollection.FirstOrDefault(x => langSelector(x)?.EqualsIgnoreCase(language) ?? langSelector(x) == language);
2538
if (result == null)
2639
{
2740
//find the first with no language set
28-
result = hasLanguages.FirstOrDefault(x => langSelector(x) == null);
41+
result = hasLanguagesCollection.FirstOrDefault(x => langSelector(x) == null);
2942
}
3043
return result;
3144
}
@@ -36,6 +49,7 @@ public static T FirstBestMatchForLanguage<T>(this IEnumerable<T> hasLanguages, F
3649
/// <param name="hasLanguages">An enumerable with values to search</param>
3750
/// <param name="language">Language to search</param>
3851
/// <returns>First matching item to the specified language</returns>
52+
[Obsolete("Use IEnumerable<IHasLanguageCode>.FirstBestMatchForLanguage()", DiagnosticId = "VC0011", UrlFormat = "https://docs.virtocommerce.org/products/products-virto3-versions")]
3953
public static IHasLanguage FirstBestMatchForLanguage(this IEnumerable<IHasLanguage> hasLanguages, string language)
4054
{
4155
return hasLanguages.FirstBestMatchForLanguage(x => x.LanguageCode, language);

0 commit comments

Comments
 (0)