views:

41

answers:

2

Hello SO;

I'm having a problem understanding how to do something in LINQ.

I have a linkedlist, the type of the object doesn't matter. What does matter is that I want to do something in a Where() based on the relationship between the current object and the next one in the list.

Why can't I do something like:

linkedlist.Where(n=>a_function(n.Value, n.Next.Value))?

What is the syntax to do this, if it's even possible? The type inference system seems to insist that I want the lambda argument to be T, not LinkedListNode<T>.

+1  A: 

Your issue doesn't have much to do with type-inference; LinkedList<T> is an IEnumerable<T> , not an IEnumerable<LinkedListNode<T>>. Additionally, there isn't a direct way to get a sequence of (current, next) tuples, so you'll have to implement that yourself.

Here's one (not so efficient) way with LINQ:

var filtered = linkedlist.Zip(linkedList.Skip(1),(current, next) => new {current, next} )
                         .Where(a => a_function(a.current, a.next))
                         .Select(a => a.current);

This will choose a value if the predicate matches (value, nextValue). You might have to tweak the query a bit if that isn't exactly what you need.

Otherwise, go with max's solution if you need efficiency or if you have lots of filters that are based on (node, nextNode).

Ani
Man, that's an ugly solution. Pretty cool if it works though, thanks. Surely there must be a nicer way, Microsoft?
Tom W
+2  A: 

You'll have to write new iterator for linked list to do that. Something like

public static class LinkedListExtensions
{
    public static IEnumerable<LinkedListNode<T>> EnumerateNodes<T>(this LinkedList<T> list)
    {
        var node = list.First;
        while(node != null)
        {
            yield return node;
            node = node.Next;
        }
    }
}

so you can use

linkedlist.EnumerateNodes().Where(n=>a_function(n.Value, n.Next.Value))
max
Thanks, this does the job nicely. I'm a bit disappointed that there's no functionality for this out of the box. I would guess that you could create quite a lot of horrible endlessly repeating loops with this sort of thing, so that's one reason it might not feature. I'm wondering whether what I'm doing is a smell, if I have to kludge it like this.
Tom W