.Take(5) 在 ObservableCollection 上返回 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; }
}
}
Edit: Actual code uploaded
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果集合中没有数据,
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 ofSkip(Count - x).Take(x)
.