score:1
An inefficient but visually aesthetic way of doing it:
public static IEnumerable<T> Nearby<T>(this IEnumerable<T> source,
int selectedIndex, int itemsToTake)
{
var left = source.Take(selectedIndex).Reverse().Take(itemsToTake / 2).Reverse();
var middle = source.ElementAt(selectedIndex);
var right = source.Skip(selectedIndex).Skip(1).Take(itemsToTake / 2);
return left.Append(middle).Concat(right);
}
Usage example:
var source = Enumerable.Range(0, 10);
Console.WriteLine($"Result: {String.Join(", ", source.Nearby(5, 5))}");
Output:
Result: 3, 4, 5, 6, 7
score:0
First, make sure that the list contains enough elements:
if(itemsToTake + 1 > List.Count)
return List.ToList(); //copy the list
The first index you want to take is (not considering sequence borders):
var firstIndex = selectedIndex - itemsToTake / 2;
The corresponding last index would be firstIndex + n
.
Then, make sure that the range is valid
if(firstIndex < 0)
firstIndex = 0;
if(firstIndex + nitemsToTake >= List.Count)
firstIndex = List.Count - 1 - itemsToTake ;
And finally do as you tried:
return List.Skip(firstIndex).Take(itemsToTake + 1).ToList();
score:0
You need to handle the special case of when selectedIndex - itemsToTake / 2
is less than 0:
public static List<T> Take<T>(this List<T> list, int selectedIndex, int itemsToTake) {
if (selectedIndex - n / 2 < 0) {
return list.Take(itemsToTake + 1).ToList();
}
return list.Skip(selectedIndex - itemsToTake / 2).Take(itemsToTake +1).ToList();
}
score:0
public static IEnumerable<T> Nearby<T>(IEnumerable<T> source, int selectedIndex, int itemsToTake)
{
itemsToTake = ((itemsToTake/2)*2)+1;
Queue<T> queue = new Queue<T>();
bool foundItem = false;
int afterItemCount = 0;
int recommendedAfterItemCount = itemsToTake/2;
foreach(var pair in source.Select((t, i) => new {t, i}))
{
T t = pair.t;
int i = pair.i;
queue.Enqueue(t);
if (itemsToTake < queue.Count) queue.Dequeue();
if (i == selectedIndex) foundItem = true;
else if (foundItem) afterItemCount += 1;
bool enoughItems = queue.Count == itemsToTake;
bool enoughAfterItems = recommendedAfterItemCount <= afterItemCount;
if (enoughItems && enoughAfterItems) break;
}
foreach(T t in queue)
{
yield return t;
}
}
score:1
Skip + Take should work fine, try this:
int firstIndex = selectedIndex - itemsToTake / 2;
firstIndex = firstIndex < 0 ? 0 : firstIndex;
return list.Skip(firstIndex).Take(itemsToTake);
Source: stackoverflow.com
Related Query
- Get n (part of) objects from a list of objects, starting from n index
- Linq code to get the index of an object in an array from an object within a list
- Get a list of objects from DataGridView?
- How to get list of objects from list of jobjects based on key exists or not?
- LINQ - Fetch items from list starting from specific index
- Using LINQ to get a list of items where the item contains a part of an item from another list
- Using linq group by to get from a list of objects with a DateTime property to a list with an interval StartDate, EndDate
- Get those elements from a List of custom class objects whose one property value is parseable to double
- Get the objects with the maximum value for a specific property from a list using LINQ
- How can I get distinct objects extracted from a property of a list of objects?
- Get an element from IQueryable<anonymous> list by index
- How to get all Objects from a List in a List with LINQ
- How to get only objects from list where property x is not null in C#
- Get distinct list objects from list using LinQ
- Get items from list where index not equal to an int using LINQ
- get sum from list of objects in linq C#
- How to get a list of objects from Entity Framework database?
- Using LINQ to get a distinct list of Parameters from a list of objects
- c# get property with specific attribute from list objects
- How to get specific set of objects from list of objects
- Get list of list of objects from linq query
- using LINQ to get a particular set of objects from a list of objects
- Get object from list of objects
- How to use LINQ to get an object from List of objects matching option from List of options
- C# Get index from List where value is in a range
- Get List of properties from a List of objects corresponding to another property
- Get ranges from List of objects with no gaps:
- c# Linq or code to extract groups from a single list of source data
- Get a list of objects from DB, create a ViewModel and populate its property by single LINQ statement
- Get unique objects From List of Objects
More Query from same tag
- Find Biggest Row By Two Values LINQ
- A temporary table using LINQ
- Multiple Left Join with Linq and Defaults Values
- Shelling an item from a list item
- add nested group by query Linq to property model class ASP.NET
- Linq filter by time range
- Create lambda expression for OrderBy based on string
- How can I get running totals of integer values from a List()?
- Select items with a frequency of 1 from a List<T> using LINQ
- c# groupby orderby thenby Try specifying the type arguments explicitly
- Using LINQ's Except<> method with an object from an external API
- How to query and return all documents in documentdb using linq
- Find an attribute that contains a ceratin string and modifying its Value in c# using LINQ
- Sort on unknown object.property WPF
- Encapsulate LINQ query for different Repositories to avoid repeated code
- C# import XML file character error
- Entity Framework INNER JOIN with "BETWEEN" date range
- Simplified Linq for return to View Model
- SQL query in linq pseudocode
- Actions on different branching criteria LINQ
- Pattern Match on all words not just whole
- Distinct on Multiple Columns Entity Framework LINQ
- How can I assign an auto-incrementing ID per group in LINQ?
- "Most popular" GROUP BY in LINQ?
- performance of LINQ queries against the SQL equivalent
- NHibernate LINQ query with GroupBy
- Display only Allow Values for User in Linq query
- Concatenation of IEnumerable collections C#
- List A value compared to all possible in List B
- Dynamic Linq on method called with generic type via reflection