Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Hashset check count before enumerating using Linq

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, Func3 func)
at Program.Main() :line 21

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Like this? signalsobject.Aggregate("", (a, b) => a + " " + b)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading