LinkedList 上的 LINQ - 迭代 LinkedListNode,而不是 T

发布于 2024-09-28 21:14:26 字数 330 浏览 1 评论 0 原文

我在理解如何在 LINQ 中执行某些操作时遇到问题。

我有一个链表,对象的类型并不重要。重要的是我想根据当前对象与列表中下一个对象之间的关系在 Where() 中执行某些操作。

为什么我不能执行类似以下操作:

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

如果可能的话,执行此操作的语法是什么?类型推断系统似乎坚持认为我希望 lambda 参数为 T,而不是 LinkedListNode

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>.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

╰ゝ天使的微笑 2024-10-05 21:14:26

您必须为链表编写新的迭代器才能做到这一点。像

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;
        }
    }
}

这样你就可以使用

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

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))
合约呢 2024-10-05 21:14:26

您的问题与类型推断没有太大关系; LinkedListIEnumerable ,而不是 IEnumerable>。此外,没有直接的方法来获取(当前,下一个)元组序列,因此您必须自己实现。

这是 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);

如果谓词匹配 (value, nextValue),这将选择一个值。如果这不完全是您所需要的,您可能需要稍微调整一下查询。

否则,如果您需要效率或者您有很多基于 (node, nextNode) 的过滤器,请使用 max 的解决方案。

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).

醉态萌生 2024-10-05 21:14:26

受到 max 答案的启发,我想出了一个较短的版本:

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

或者您可以牺牲可读性以获得更短的版本:

public static IEnumerable<LinkedListNode<T>> GetNodes<T>(this LinkedList<T> list)
    => for (var node = list.First; node != null; node = node.Next) yield return node;

Inspired by max's answer, I came up with a shorter version:

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

Or you could sacrifice readability for an even shorter version:

public static IEnumerable<LinkedListNode<T>> GetNodes<T>(this LinkedList<T> list)
    => for (var node = list.First; node != null; node = node.Next) yield return node;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文