score:10
Accepted answer
You can use this iterator block:
IEnumerable<int> CreateRange(int x1, int x2) {
var increment = x2 > x1 ? 1 : -1;
for (var i = x1; i != x2; i += increment)
yield return i;
yield return x2;
}
It's efficient and creating a range is just as clean as using Enumerable.Range
:
CreateRange(10, 5).Select( ... )
score:3
You can do something like this:
//normal order works
int x1 = 5;
int x2 = 10;
int len = Math.Abs(x2 - x1) + 1;
var range = x1 < x2 ? Enumerable.Range(x1, len) : Enumerable.Range(x2, len).Reverse();
//reverse order works (the same code for range)
x1 = 10;
x2 = 5;
len = Math.Abs(x2 - x1) + 1;
range = x1 < x2 ? Enumerable.Range(x1, len) : Enumerable.Range(x2, len).Reverse();
score:4
If you want to stick with single LINQ statement -
Enumerable.Range(0, Math.Abs(x2 - x1) + 1).Select(i => x1 + i * Math.Sign(x2 - x1))
Source: stackoverflow.com
Related Articles
- Better Enumerable.Range for both ascending and descending ranges?
- Enumerable range in descending order
- Better way to sort array in descending order
- Linq expression to validate if list is ordered ascending or descending
- LINQ query needs either ascending or descending in the same query
- Sort part of a list in descending order (by date), the other part in ascending order (alphabetically)?
- LINQ Source Code Available
- Sorting fields ascending and descending with single link
- .NET 4 Code Contracts: "requires unproven: source != null"
- Removing a single item from an enumerable source when the items are equal
- creating Linq to sqlite dbml from DbLinq source code
- Better code for avoiding one dictionary - Case Sensitivity Issue
- Linq Query to join overlapping ranges and construct a new continuous range
- C# Using Enumerable Range and Except with custom class to determine missing sequence number
- Accessing a List multiple times in same code block - Any better approach?
- How to get enumerable range with a variable step count
- Choosing better query out of 2 queries, both returning same results
- Using only Linq or Lambda, how do I combine both pieces of code to return List<Entity> e?
- source code for LINQ 101 samples
- Better to use static Enumerable FirstOrDefault over the object's method?
- How to code this LINQ query in a better way
- Is there a better way to code this LINQ fragment?
- Ascending and descending of list object in c# using LINQ
- C# LINQ Find List Inside Another List, Better way to code this than a foreach loop
- Is there a better way to code this Duplicate ID Checker?
- Better Code in LinQ?
- Sorting Linq results ascending using subselect and then descending
- Union 2 lists with additional content change for ID that occurs in both source lists
- Sorting a list in ascending and descending order
- Is there a better way to achieve this with Linq or a better code
- LINQ XML query: How do I perform query for binding?
- System.Xml.Linq.XElement>' does not contain a definition for 'First' and no extension method 'First' accepting a first argument of
- Double(,) and not Double()() from DataTable Using Select
- Find and map at the same time
- Creating a select list from an array
- Entity Framework GroupJoin different result vs join with group
- LinQ OrderBy specific element in sublist
- Dicionary intersect keys with list and get in result dicionary with values at the beggining LINQ
- Is it possible to store Math expressions in a database with EF C# Linq
- How do I use LINQ to select from DataTable, with a list of the required column names
- What's the best practice for having 4 loops over same data in an MVC 4 view?
- Linq let and if
- how to search LINQ SQL where value in LIST<object>
- Linq Query Giving Error
- Linq query to order a list based on date and get distinct
- Clean way to Compare / Copy between Similar Classes
- Cast to value type 'Enum' failed as the materialized value is null. Either the result type's param or query must use nullable type
- Issue with ViewModel returning null values to View
- Refactoring method that binds controls to LINQ
- C# extension of Enumerable not working