我有这门课
public class ConnectionResult
{
private int connectionPercentage;
public int ConnectPercentage
{
get { return connectionPercentage; }
}
public ConnectionResult(int ip)
{
// Check connection and set connectionPercentage
}
}
我有一个管理器,它获取多个 ConnectionResult 列表,并计算每个值大于配置确定的特定数字。我的实现是这样的:
public class CurrentConnections
{
private static CurrentConnections inst;
private CurrentConnections()
{
}
public static CurrentConnections GetInstance
{
get
{
if (inst != null)
{
inst = new CurrentConnections();
}
return inst;
}
}
public int CountActiveConnections(params List<ConnectionResult>[] conns)
{
int rtVal = 0;
foreach (List<ConnectionResult> connectionResult in conns)
{
foreach (var currConn in connectionResult)
{
if (currConn.ConnectPercentage > ACCEPTABLE_CONNECTION)
{
rtVal++;
}
}
}
return rtVal;
}
}
但我想让它变得更好,所以我开始用 linq 写它,我必须
conns.Count(x => x.Count(y => y.ConnectPercentage > ACCEPTABLE_CONNECTION));
但这给了我一个错误Cannot implicitly convert type 'int' to 'bool'
。有没有办法在 linq 中计算它,还是我必须坚持我写的内容?顺便说一句,我是 linq 的新手