Last active
October 18, 2023 16:59
-
-
Save sean-m/da66e98f79ceb5f63ccad8803e7a5b94 to your computer and use it in GitHub Desktop.
Extension method that will yield properties from a specified group of properties if any of them are encountered in the first collection. It's using the group metaphore because I use it for grouping properties together by name in a UI. Yes there's perf issues as things get big, it's your foot.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Let's say you have a collection of things with an indeterminate ordering but when you | |
/// enumerate the things, you'd like a subset to be grouped together. That's what this | |
/// can do but there's no guarantee elements in the group parameter exist in the primaryCollection. | |
/// That can be seen as a bug or a feature, it's up to you. | |
/// </summary> | |
/// <typeparam name="T"></typeparam> | |
/// <param name="primaryCollection"></param> | |
/// <param name="group"></param> | |
/// <returns></returns> | |
public static IEnumerable<T> OrderedGroupAtFirstOccurance<T>(this IEnumerable<T> primaryCollection, IEnumerable<T> group) { | |
bool groupEnumerated = false; | |
foreach (var i in primaryCollection) { | |
if (group.Contains(i)) { | |
if (!groupEnumerated) { | |
foreach (var t in group) { | |
yield return t; | |
} | |
groupEnumerated = true; | |
} | |
} else { | |
yield return i; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment