.Take(5) 在 ObservableCollection 上返回 0 计数

发布于 2024-11-03 04:01:32 字数 1934 浏览 1 评论 0原文

我尝试使用 LINQ 从 ObservableCollection 返回前 5 个 ping 结果,但生成的 IEnumerable 计数为 0。

任何人都可以解释为什么 <当 .Take(5) 应用于 PingReplies 时,下面代码中的 code>lastFive 对象返回计数 0?

发送 ping 时,PingReplies 集合会获取 ObservableCollection 中的该对象

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.NetworkInformation;
using System.Collections.ObjectModel;

namespace XXX.ServerMonitor.Servers
{
    class WindowsServer : IServer
    {
        public WindowsServer(string address)
        {
            this.Address = address;
            PingReplies = new ObservableCollection<PingReply>();
            PingReplies.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(PingReplies_CollectionChanged);
        }

        void PingReplies_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
            {
                IEnumerable<PingReply> lastFive = PingReplies.Take(5);
                if (lastFive.Where(a => a.Status != IPStatus.Success).Count() == 5)
                {
                    // 5 failed attempts
                    // Server may be down
                    Console.WriteLine(Address + " may be down");
                }
            }
        }

        public ObservableCollection<PingReply> PingReplies { get; set; }

        PingReply IServer.Ping()
        {
            PingReply reply = Utils.Ping.Send(this.Address);
            PingReplies.Add(reply);
            return reply;
        }

        public string Address { get; set; }
    }
}

“在此处输入图像描述”

编辑:上传的实际代码

I'm trying to use LINQ to return the top 5 ping results from an ObservableCollection<PingReply> but the resulting IEnumerable has a count of 0.

Can anyone explain why the lastFive object in the code below returns a count of 0 when .Take(5) is applied to PingReplies?

When a ping is sent, the PingReplies collection get's that object in the ObservableCollection

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.NetworkInformation;
using System.Collections.ObjectModel;

namespace XXX.ServerMonitor.Servers
{
    class WindowsServer : IServer
    {
        public WindowsServer(string address)
        {
            this.Address = address;
            PingReplies = new ObservableCollection<PingReply>();
            PingReplies.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(PingReplies_CollectionChanged);
        }

        void PingReplies_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
            {
                IEnumerable<PingReply> lastFive = PingReplies.Take(5);
                if (lastFive.Where(a => a.Status != IPStatus.Success).Count() == 5)
                {
                    // 5 failed attempts
                    // Server may be down
                    Console.WriteLine(Address + " may be down");
                }
            }
        }

        public ObservableCollection<PingReply> PingReplies { get; set; }

        PingReply IServer.Ping()
        {
            PingReply reply = Utils.Ping.Send(this.Address);
            PingReplies.Add(reply);
            return reply;
        }

        public string Address { get; set; }
    }
}

enter image description here

Edit: Actual code uploaded

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

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

发布评论

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

评论(1

秋心╮凉 2024-11-10 04:01:32

如果集合中没有数据,Take 不会返回任何项目。如果确实存在一些数据,那么您一定在未向我们展示的代码中犯了一些错误。请记住:select 没有损坏...

顺便说一下,还有一个Reverse,而不是Skip(Count - x).Take(x)

If there is no data in the collection, Take returns no items. If there is actually some data around, you must have made some mistake in code you aren't showing us. Remember: select ain't broken ...

by the way, there is also a Reverse, instead of Skip(Count - x).Take(x).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文