You can create methods like this:
public static IEnumerable<int> getIntsWithinN(int BaseInt, int Offset) { return getIntsWithinN(Enumerable.Empty<int>(), BaseInt, Offset); } public static IEnumerable<int> getIntsWithinN(this IEnumerable<int> source, int BaseInt, int Offset) { return source.Concat(Enumerable.Range(BaseInt - Offset, Offset * 2 + 1)); }
Used as follows:
var myBigList = Extensions.getIntsWithinN(7, 3).getIntsWithinN(42, 7);
And then depending on how you want from there, for example
var withDupsRemoved = new HashSet<int>(myBigList); var withDupsRemoved = new HashSet<int>(myBigList).OrderBy(x => x).ToList();
If the insertion order of the ranges is important, but you do not want duplicates, you can make a list like this:
var withDupsRemoved = new List<int>(); foreach (var i in myBigList) if (!withDupsRemoved.Contains(i)) withDupsRemoved.Add(i);
If the performance of this becomes a problem, instead of using LINQ Concat, you can roll up your own class that stores range boundaries and when two or more are added, if there is overlap, ranges are adjusted accordingly.
source share