score:6
i think maybe you can change the line
return (index + 1 == items.count) ? items[0] : items[index + 1];
for something like
return items[(index + 1) % items.count];
score:0
you can use the mod operator to simplify this a bit and combine everything into one statement:
return items[((string.isnullorwhitespace(curr)
? 0
: items.indexof(curr)) + 1) % items.count]
its definitely shorter, but i'm not sure weather its more readable, too :)
score:0
i think the best solution is in the below link, i tried it out and works like charm.
score:1
i can see some optimization if you track the current index rather than the current string, but to do that the list of items would have to be fixed, i.e. not change.
you could also return items[(index + 1) % items.count];
otherwise that code looks fine to me, but perhaps someone has a more clever solution.
score:1
linq is not the appropriate tool here.
it sounds as if a linkedlist<t>
would be the better collection here:
var linkeditems = new linkedlist<string>(items);
linkedlistnode current = linkeditems.find("c");
string afterc = current.next == null ? linkeditems.first.value : current.next.value;
here are the pros and cons of a linkedlist compared to a list.
score:1
a linq way:
var result = (from str in list
let index = list.indexof(curr) + 1
select list.elementatordefault(index) ?? list[0]).first();
score:7
the solution you have is functionally correct but it's performance leaves a little to be desired. typically when dealing with a list style structure you would expect that getnext
would return a result in o(1) time yet this solution is o(n).
public sealed class wrappingiterator<t> {
private ilist<t> _list;
private int _index;
public wrappingiterator<t>(ilist<t> list, int index) {
_list = list;
_index = index;
}
public t getnext() {
_index++;
if (_index == _list.count) {
_index = 0;
}
return _list[_index];
}
public static wrappingiterator<t> createat(ilist<t> list, t value) {
var index = list.indexof(value);
return new wrappingiterator(list, index);
}
}
the initial call to createat
is o(n) here but subsequent calls to getnext
are o(1).
ilist<string> list = ...;
var iterator = wrappingiterator<string>.createat(list, "b");
console.writeline(iterator.getnext()); // prints c
console.writeline(iterator.getnext()); // prints a
console.writeline(iterator.getnext()); // prints b
Source: stackoverflow.com
Related Query
- Is there a better way to return the next item in a list and loop from the end to the front?
- EF Core query - is there a way to find the next and previous item alphabetically?
- How to execute a linq query for each item in a list , use it in the where clause and return a collection from the result of each query?
- Is there a shorter, better and optimised way to fill this list from SQL database
- Is there a better way to do this, maybe replace the for/foreach loop with something LINQ ish?
- what is the most elegant way to update an item in one list from another list in C#?
- If you have a LINQ statement using the Select() method, is there a way to get a value from the next record?
- Is there a Linq operation to retrieve specific items from a list of items where the item has a property value for a property which should be unique?
- Is there a better way to get the column values from datatable
- Remove duplicate items from list if certain properties match and get the top item based on some ordering
- Get next item in list c# and update the current one
- Is there a better way to roll up child data into a list outside of the database?
- How to query the average from a certain property in an Entity and return it in a List of List of objects in LINQ?
- Is there a way to get all the items from a list that have a specific property
- C# LINQ Find List Inside Another List, Better way to code this than a foreach loop
- Is there a way to initialize a list of elements from an array and integer value?
- Is there a way I can check every item in a list and produce a result based on that check?
- is there a better performance's way to get only few columns in ASP MVC without getting the whole table and filter it with LinQ?
- Got a value from a list and want to get the id of the item having the list as property
- C# Linq Comparing List Values using multiple fields and return item which doesn't satisfy the condition
- Return 2 properties from a list using lambda ID(distinct) and the count of the specific ID?
- Is there a way to update a generic list using LINQ rather than deleting a member of the list and adding it back?
- C# Check if list<t> contains any of another list and return the matching item
- Is there a way to return a list of records which match an attribute of the first query filter result using LINQ?
- C# is there a better way of adding an element from a list in another class?
- Comparing two lists and removing each item that contains an entry from the other list
- Join 2 collection list together from 2 data sources find the matches and loop through the results
- How to check if all list items have the same value and return it, or return an “otherValue” if they don’t?
- How can I filter a dictionary using LINQ and return it to a dictionary from the same type
- Clean the string? is there any better way of doing it?
More Query from same tag
- LINQ to return true if collection contains element satisfying predicate
- How does a streaming operator differ from deferred execution?
- IQueryable/Linq Orderby Fail: Sorts only a limited number of records
- Selecting Tuples from ILookup throws exception
- LINQ query to get data from a table connected by three tables away
- Find the exact week count based on date range
- "A lambda expression with a statement body cannot be converted to an expression tree"
- F# group by more than one value and aggregate
- Sort an XML preserving whitespace/indentation?
- LINQ to SQL cross apply
- Combine two list of decimals in one array
- NotSupportedException thrown after call to .AsEnumerable()
- C# - Compare DateTime without Time on LINQ?
- How to properly union different IQueryables and then serialize them in JSON
- Speeding up my simple Redis .NET application
- How can I add and remove records from a collection with LINQ
- Filter all the record having navigation property in Linq .Net MVC
- How to resolve multiple join with LinQ?
- This method is not supported against a materialized query result
- How to trim in Linq to SQL?
- System.Data.Entity.QueryableExtensions.Include : Object reference not set to an instance
- How to Select then OrderBy using LINQ?
- How to improve LINQ repository query in MVC 4
- foreach in linq result not working
- Linq Get Data Based On Single Column Value
- Populating a C# / XAML combobox using EF and LINQ
- Sort on list of objects that contains dictionary
- using <T>(T obj) with a Linq query
- LINQ to DataSet Group By Multiple Columns - Method Syntax
- Linq expressions syntax and Compile