0

Suppose I have a list of objects called TheListOfMyObjects. In that list, there's an object with a property that matches a value: MyObject.TheProperty = SomeValue.

How can I get the previous 3 objects and the next 3 objects from within this list?

Note that if the object that matches SomeValue is in first position then I'd need the 3 objects in position 2-5 and the 3 objects in the last 3 positions for a total of 6 objects.

Thanks for your suggestions.

CrazyCasta
  • 26,917
  • 4
  • 45
  • 72
frenchie
  • 51,731
  • 109
  • 304
  • 510
  • 1
    [What have you tried](http://whathaveyoutried.com)? – Oded Nov 04 '12 at 19:25
  • I tried converting the list to an array by pushing every element into an array with a foreach loop. Now I've got an array with indexes but I am wondering if there's a shorter linq based approach. – frenchie Nov 04 '12 at 19:28
  • If you are using an `IList` you can access it by index already. With LINQ, you can look at using `Take` for this (provided you have the initial index). But you didn't mention either the type of the list variable nor that you would like a LINQ implementation. – Oded Nov 04 '12 at 19:30
  • It's actually a list of timezone objects. var TheListOfAllTimezones = TimeZoneInfo.GetSystemTimeZones(); – frenchie Nov 04 '12 at 19:31
  • what you're looking for is a circular list. See this: http://stackoverflow.com/questions/716256/creating-a-circually-linked-list-in-c – Hardrada Nov 04 '12 at 20:10

2 Answers2

3

Try this:

var theObj = TheListOfMyObjects.First(x => x.TheProperty == someValue);
var index = TheListOfMyObjects.IndexOf(theObj);
//and from there it's obvious.

In case there's a chance the list won't contain such an element, use FirstOrDefault and check for null on theObj.

nieve
  • 4,601
  • 3
  • 20
  • 26
  • 1
    Note that this is only correct iff the object uses (default) reference equality. Otherwise this can lead to both negative and positive surprises. See IEQuatable – sehe Nov 04 '12 at 19:32
  • 1
    Ok, thanks; this gave me an idea: just add another list to the same existing list that way I won't have to deal with the edge cases where the value is at the beginning or the end of the list. – frenchie Nov 04 '12 at 19:34
1

Sorry for the late answer, I had problems with the computer. You don't have to add another list, you can solve edge cases with something like this if you have at least 7 objects in your list:

            MyObject mo = list.FirstOrDefault(x => x.TheProperty.Equals(SomeValue));
            if(mo != null)
            {
                int index = list.IndexOf(mo);
                MyObject moMinus3 = list[(index - 3 + list.Count) % list.Count];
                MyObject moMinus2 = list[(index - 2 + list.Count) % list.Count];
                MyObject moMinus1 = list[(index - 1 + list.Count) % list.Count];
                MyObject mo0 = list[index];
                MyObject moPlus1 = list[(index + 1 + list.Count) % list.Count];
                MyObject moPlus2 = list[(index + 2 + list.Count) % list.Count];
                MyObject moPlus3 = list[(index + 3 + list.Count) % list.Count];
            }
Nikola Davidovic
  • 8,556
  • 1
  • 27
  • 33