Simplify Converting List to SortedSet
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#
|
show 4 more comments
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#
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 forToList()
instead of genericToCollection<T>() where T: ICollection<T>
method.
– Joker_vD
Nov 14 '18 at 7:13
|
show 4 more comments
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#
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#
c#
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 forToList()
instead of genericToCollection<T>() where T: ICollection<T>
method.
– Joker_vD
Nov 14 '18 at 7:13
|
show 4 more comments
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 forToList()
instead of genericToCollection<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
|
show 4 more comments
1 Answer
1
active
oldest
votes
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();
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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();
add a comment |
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();
add a comment |
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();
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();
edited Nov 14 '18 at 0:55
answered Nov 14 '18 at 0:50
JohnJohn
12k32038
12k32038
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 genericToCollection<T>() where T: ICollection<T>
method.– Joker_vD
Nov 14 '18 at 7:13