How can i look for count before using linq to enumerate a Hashset? If teh count is zero, i want to return empty 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";
}
}
I currently get
[System.InvalidOperationException: Sequence contains no elements]
at System.Linq.Enumerable.Aggregate[TSource](IEnumerable1 source, Func
3 func)
at Program.Main() :line 21
CodePudding user response:
Like this?
signalsobject.Aggregate("", (a, b) => a " " b)