使用 Linq 枚举之前的哈希集检查计数
Hashset check count before enumerating using Linq
在使用 linq 枚举哈希集之前如何查找计数?如果计数为零,我想 return 清空
code fiddle
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
private static HashSet<string> signalsobject = new HashSet<string>();
public enum Signals : long
{
SHORT20EMASIGNAL = 2 ^ 7,
DOUBLESHORTSIGNAL = 2 ^ 8,
}
;
public static void Main()
{
signalsobject.Add(Signals.SHORT20EMASIGNAL.ToString());
signalsobject.Add(Signals.SHORT20EMASIGNAL.ToString());
signalsobject.Remove(Signals.SHORT20EMASIGNAL.ToString());
Console.WriteLine(signalsobject.Count);
Console.WriteLine(signalsobject.Aggregate((a, b) => a + " " + b));
//Console.WriteLine(signalsobject.ToString());
// string a = signalsobject.Count>0? (signalsobject.Aggregate((a, b) => a + " " + b)): "Empty";
}
}
我目前得到
[System.InvalidOperationException: 序列不包含任何元素]
在 System.Linq.Enumerable.Aggregate[TSource](IEnumerable1 source, Func
3 函数)
在 Program.Main() 处:第 21 行
像这样?
signalsobject.Aggregate("", (a, b) => a + " " + b)
在使用 linq 枚举哈希集之前如何查找计数?如果计数为零,我想 return 清空 code fiddle
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
private static HashSet<string> signalsobject = new HashSet<string>();
public enum Signals : long
{
SHORT20EMASIGNAL = 2 ^ 7,
DOUBLESHORTSIGNAL = 2 ^ 8,
}
;
public static void Main()
{
signalsobject.Add(Signals.SHORT20EMASIGNAL.ToString());
signalsobject.Add(Signals.SHORT20EMASIGNAL.ToString());
signalsobject.Remove(Signals.SHORT20EMASIGNAL.ToString());
Console.WriteLine(signalsobject.Count);
Console.WriteLine(signalsobject.Aggregate((a, b) => a + " " + b));
//Console.WriteLine(signalsobject.ToString());
// string a = signalsobject.Count>0? (signalsobject.Aggregate((a, b) => a + " " + b)): "Empty";
}
}
我目前得到
[System.InvalidOperationException: 序列不包含任何元素]
在 System.Linq.Enumerable.Aggregate[TSource](IEnumerable1 source, Func
3 函数)
在 Program.Main() 处:第 21 行
像这样?
signalsobject.Aggregate("", (a, b) => a + " " + b)