I have come across a situation where I need to show the previous and next record of a matching record , suppose I have a data source for products and the products are sorted by name . Now after retrieving the product by id I also want to retrieve the very next and previous product but in alphabetic order .
How can I achieve this using LINQ ?
3
Answers
Sort the list in alphabetic order, find the index of your entry and look for the entries with index+/-1.
more efficient way without sorting the list:
An efficient non-LINQ answer, assuming the sequence is pre-sorted.