. Net – LINQ on LinkedList – iterates linkedlistnode instead of T
•
Java
I'm having trouble understanding how to perform certain operations in LINQ
I have a linked list. The type of object doesn't matter The important thing is that I want to do something in where () based on the relationship between the current object and the next object in the list
Why can't I do this:
linkedlist. Where(n => a_function(n.Value,n.Next.Value))?
If possible, what is the syntax for doing so? The type inference system seems to insist that I want the lambda parameter to be t, not linkedlistnode < T >
Solution
You must create a new iterator for the linked list to do this It's 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))
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
