Simplify Converting List to SortedSet












3















I am trying to simplify my solution below where I am trying to convert a List<DateTime> to SortedSet<long>. I am wondering if this is even possible?



List<DateTime> dateTimes = new List<DateTime>();
dateTimes.Add(...);

// simplify the 5 lines below into 1 line
SortedSet<long> timestamps = new SortedSet<long>();
foreach(DateTime dateTime in dateTimes)
{
timestamps.Add(convertDateTimeToTimestamp(dateTime));
}


I have been able to convert a List<float> to List<double> via:



List<float> average = new List<float>();
average.Add(...);
List<double> newAverage = average.Select(x => (double?)x).ToList();


I however was unable to find a .ToSet() or .ToSortedSet() method.










share|improve this question























  • There is a .ToHashSet() method, but not a .ToSortedSet() method.

    – John
    Nov 14 '18 at 1:00











  • @John Ahh, thank you. Is there a reason why there would not be a .ToSortedSet()?

    – Jon
    Nov 14 '18 at 1:04











  • On looking more closely, it seems that .ToHashSet() doesn't exist in .NET Framework, but does in .NET Core and presumably .NET Standard. It seems like a new addition. Maybe they will add more in future.

    – John
    Nov 14 '18 at 1:06






  • 2





    @Jon: The reason why there is not a feature you want is always the same: No one implemented that feature. In order for you to use a feature, someone has to implement it. You didn't implement it, and no one else did either. Why didn't you implement it?

    – Eric Lippert
    Nov 14 '18 at 1:30











  • @EricLippert To be frank, I am surprised .NET went for ToList() instead of generic ToCollection<T>() where T: ICollection<T> method.

    – Joker_vD
    Nov 14 '18 at 7:13
















3















I am trying to simplify my solution below where I am trying to convert a List<DateTime> to SortedSet<long>. I am wondering if this is even possible?



List<DateTime> dateTimes = new List<DateTime>();
dateTimes.Add(...);

// simplify the 5 lines below into 1 line
SortedSet<long> timestamps = new SortedSet<long>();
foreach(DateTime dateTime in dateTimes)
{
timestamps.Add(convertDateTimeToTimestamp(dateTime));
}


I have been able to convert a List<float> to List<double> via:



List<float> average = new List<float>();
average.Add(...);
List<double> newAverage = average.Select(x => (double?)x).ToList();


I however was unable to find a .ToSet() or .ToSortedSet() method.










share|improve this question























  • There is a .ToHashSet() method, but not a .ToSortedSet() method.

    – John
    Nov 14 '18 at 1:00











  • @John Ahh, thank you. Is there a reason why there would not be a .ToSortedSet()?

    – Jon
    Nov 14 '18 at 1:04











  • On looking more closely, it seems that .ToHashSet() doesn't exist in .NET Framework, but does in .NET Core and presumably .NET Standard. It seems like a new addition. Maybe they will add more in future.

    – John
    Nov 14 '18 at 1:06






  • 2





    @Jon: The reason why there is not a feature you want is always the same: No one implemented that feature. In order for you to use a feature, someone has to implement it. You didn't implement it, and no one else did either. Why didn't you implement it?

    – Eric Lippert
    Nov 14 '18 at 1:30











  • @EricLippert To be frank, I am surprised .NET went for ToList() instead of generic ToCollection<T>() where T: ICollection<T> method.

    – Joker_vD
    Nov 14 '18 at 7:13














3












3








3








I am trying to simplify my solution below where I am trying to convert a List<DateTime> to SortedSet<long>. I am wondering if this is even possible?



List<DateTime> dateTimes = new List<DateTime>();
dateTimes.Add(...);

// simplify the 5 lines below into 1 line
SortedSet<long> timestamps = new SortedSet<long>();
foreach(DateTime dateTime in dateTimes)
{
timestamps.Add(convertDateTimeToTimestamp(dateTime));
}


I have been able to convert a List<float> to List<double> via:



List<float> average = new List<float>();
average.Add(...);
List<double> newAverage = average.Select(x => (double?)x).ToList();


I however was unable to find a .ToSet() or .ToSortedSet() method.










share|improve this question














I am trying to simplify my solution below where I am trying to convert a List<DateTime> to SortedSet<long>. I am wondering if this is even possible?



List<DateTime> dateTimes = new List<DateTime>();
dateTimes.Add(...);

// simplify the 5 lines below into 1 line
SortedSet<long> timestamps = new SortedSet<long>();
foreach(DateTime dateTime in dateTimes)
{
timestamps.Add(convertDateTimeToTimestamp(dateTime));
}


I have been able to convert a List<float> to List<double> via:



List<float> average = new List<float>();
average.Add(...);
List<double> newAverage = average.Select(x => (double?)x).ToList();


I however was unable to find a .ToSet() or .ToSortedSet() method.







c#






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 0:49









JonJon

2,933165998




2,933165998













  • There is a .ToHashSet() method, but not a .ToSortedSet() method.

    – John
    Nov 14 '18 at 1:00











  • @John Ahh, thank you. Is there a reason why there would not be a .ToSortedSet()?

    – Jon
    Nov 14 '18 at 1:04











  • On looking more closely, it seems that .ToHashSet() doesn't exist in .NET Framework, but does in .NET Core and presumably .NET Standard. It seems like a new addition. Maybe they will add more in future.

    – John
    Nov 14 '18 at 1:06






  • 2





    @Jon: The reason why there is not a feature you want is always the same: No one implemented that feature. In order for you to use a feature, someone has to implement it. You didn't implement it, and no one else did either. Why didn't you implement it?

    – Eric Lippert
    Nov 14 '18 at 1:30











  • @EricLippert To be frank, I am surprised .NET went for ToList() instead of generic ToCollection<T>() where T: ICollection<T> method.

    – Joker_vD
    Nov 14 '18 at 7:13



















  • There is a .ToHashSet() method, but not a .ToSortedSet() method.

    – John
    Nov 14 '18 at 1:00











  • @John Ahh, thank you. Is there a reason why there would not be a .ToSortedSet()?

    – Jon
    Nov 14 '18 at 1:04











  • On looking more closely, it seems that .ToHashSet() doesn't exist in .NET Framework, but does in .NET Core and presumably .NET Standard. It seems like a new addition. Maybe they will add more in future.

    – John
    Nov 14 '18 at 1:06






  • 2





    @Jon: The reason why there is not a feature you want is always the same: No one implemented that feature. In order for you to use a feature, someone has to implement it. You didn't implement it, and no one else did either. Why didn't you implement it?

    – Eric Lippert
    Nov 14 '18 at 1:30











  • @EricLippert To be frank, I am surprised .NET went for ToList() instead of generic ToCollection<T>() where T: ICollection<T> method.

    – Joker_vD
    Nov 14 '18 at 7:13

















There is a .ToHashSet() method, but not a .ToSortedSet() method.

– John
Nov 14 '18 at 1:00





There is a .ToHashSet() method, but not a .ToSortedSet() method.

– John
Nov 14 '18 at 1:00













@John Ahh, thank you. Is there a reason why there would not be a .ToSortedSet()?

– Jon
Nov 14 '18 at 1:04





@John Ahh, thank you. Is there a reason why there would not be a .ToSortedSet()?

– Jon
Nov 14 '18 at 1:04













On looking more closely, it seems that .ToHashSet() doesn't exist in .NET Framework, but does in .NET Core and presumably .NET Standard. It seems like a new addition. Maybe they will add more in future.

– John
Nov 14 '18 at 1:06





On looking more closely, it seems that .ToHashSet() doesn't exist in .NET Framework, but does in .NET Core and presumably .NET Standard. It seems like a new addition. Maybe they will add more in future.

– John
Nov 14 '18 at 1:06




2




2





@Jon: The reason why there is not a feature you want is always the same: No one implemented that feature. In order for you to use a feature, someone has to implement it. You didn't implement it, and no one else did either. Why didn't you implement it?

– Eric Lippert
Nov 14 '18 at 1:30





@Jon: The reason why there is not a feature you want is always the same: No one implemented that feature. In order for you to use a feature, someone has to implement it. You didn't implement it, and no one else did either. Why didn't you implement it?

– Eric Lippert
Nov 14 '18 at 1:30













@EricLippert To be frank, I am surprised .NET went for ToList() instead of generic ToCollection<T>() where T: ICollection<T> method.

– Joker_vD
Nov 14 '18 at 7:13





@EricLippert To be frank, I am surprised .NET went for ToList() instead of generic ToCollection<T>() where T: ICollection<T> method.

– Joker_vD
Nov 14 '18 at 7:13












1 Answer
1






active

oldest

votes


















2














What about using the constructor overload that takes IEnumerable<T>?:



timestamps = new SortedSet<long>(dateTimes.Select(convertDateTimeToTimestamp));


Or wrapping it up in an extension method:



namespace System.Linq
{
public static class CustomLinqExtensions
{
public static SortedSet<TSource> ToSortedSet<TSource>(this IEnumerable<TSource> source)
{
if (source == null) throw new ArgumentNullException(nameof(source));
return new SortedSet<TSource>(source);
}

public static SortedSet<TSource> ToSortedSet<TSource>(this IEnumerable<TSource> source, IComparer<TSource> comparer)
{
if (source == null) throw new ArgumentNullException(nameof(source));
return new SortedSet<TSource>(source, comparer);
}
}
}


Then you can simply call dateTimes.Select(convertDateTimeToTimestamp).ToSortedSet();






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291616%2fsimplify-converting-listdatetime-to-sortedsetlong%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    What about using the constructor overload that takes IEnumerable<T>?:



    timestamps = new SortedSet<long>(dateTimes.Select(convertDateTimeToTimestamp));


    Or wrapping it up in an extension method:



    namespace System.Linq
    {
    public static class CustomLinqExtensions
    {
    public static SortedSet<TSource> ToSortedSet<TSource>(this IEnumerable<TSource> source)
    {
    if (source == null) throw new ArgumentNullException(nameof(source));
    return new SortedSet<TSource>(source);
    }

    public static SortedSet<TSource> ToSortedSet<TSource>(this IEnumerable<TSource> source, IComparer<TSource> comparer)
    {
    if (source == null) throw new ArgumentNullException(nameof(source));
    return new SortedSet<TSource>(source, comparer);
    }
    }
    }


    Then you can simply call dateTimes.Select(convertDateTimeToTimestamp).ToSortedSet();






    share|improve this answer






























      2














      What about using the constructor overload that takes IEnumerable<T>?:



      timestamps = new SortedSet<long>(dateTimes.Select(convertDateTimeToTimestamp));


      Or wrapping it up in an extension method:



      namespace System.Linq
      {
      public static class CustomLinqExtensions
      {
      public static SortedSet<TSource> ToSortedSet<TSource>(this IEnumerable<TSource> source)
      {
      if (source == null) throw new ArgumentNullException(nameof(source));
      return new SortedSet<TSource>(source);
      }

      public static SortedSet<TSource> ToSortedSet<TSource>(this IEnumerable<TSource> source, IComparer<TSource> comparer)
      {
      if (source == null) throw new ArgumentNullException(nameof(source));
      return new SortedSet<TSource>(source, comparer);
      }
      }
      }


      Then you can simply call dateTimes.Select(convertDateTimeToTimestamp).ToSortedSet();






      share|improve this answer




























        2












        2








        2







        What about using the constructor overload that takes IEnumerable<T>?:



        timestamps = new SortedSet<long>(dateTimes.Select(convertDateTimeToTimestamp));


        Or wrapping it up in an extension method:



        namespace System.Linq
        {
        public static class CustomLinqExtensions
        {
        public static SortedSet<TSource> ToSortedSet<TSource>(this IEnumerable<TSource> source)
        {
        if (source == null) throw new ArgumentNullException(nameof(source));
        return new SortedSet<TSource>(source);
        }

        public static SortedSet<TSource> ToSortedSet<TSource>(this IEnumerable<TSource> source, IComparer<TSource> comparer)
        {
        if (source == null) throw new ArgumentNullException(nameof(source));
        return new SortedSet<TSource>(source, comparer);
        }
        }
        }


        Then you can simply call dateTimes.Select(convertDateTimeToTimestamp).ToSortedSet();






        share|improve this answer















        What about using the constructor overload that takes IEnumerable<T>?:



        timestamps = new SortedSet<long>(dateTimes.Select(convertDateTimeToTimestamp));


        Or wrapping it up in an extension method:



        namespace System.Linq
        {
        public static class CustomLinqExtensions
        {
        public static SortedSet<TSource> ToSortedSet<TSource>(this IEnumerable<TSource> source)
        {
        if (source == null) throw new ArgumentNullException(nameof(source));
        return new SortedSet<TSource>(source);
        }

        public static SortedSet<TSource> ToSortedSet<TSource>(this IEnumerable<TSource> source, IComparer<TSource> comparer)
        {
        if (source == null) throw new ArgumentNullException(nameof(source));
        return new SortedSet<TSource>(source, comparer);
        }
        }
        }


        Then you can simply call dateTimes.Select(convertDateTimeToTimestamp).ToSortedSet();







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 14 '18 at 0:55

























        answered Nov 14 '18 at 0:50









        JohnJohn

        12k32038




        12k32038






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291616%2fsimplify-converting-listdatetime-to-sortedsetlong%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine