C
C#2y ago
garrus_j

❔ Ugly foreach - can it be improved?

I'm trying to turn a list of JSON elements which are nested into singular objects. Eg Object has an id, name, date and list of elements[]. Elements has a date and timespan[]. Timespan has a time and a value. I want an object that has everything in one -> id, name, date, time, value. I've solved it with a foreach loop but I feel like it's not optimal, any feedback appreciated!
19 Replies
Angius
Angius2y ago
I see the reason why it's ugly, your colour scheme mweh said, the only way I can see you can clean it up, is by using some LINQ Without LINQ, it's about as good as it can get I honestly have no idea how you can read text with so little contrast White on black works great, black on white also works... pastel on gray tho?
garrus_j
garrus_jOP2y ago
Alright, LINQ here i come. Thanks! it was the default on ray.so
Angius
Angius2y ago
tf is ray.so
garrus_j
garrus_jOP2y ago
definitely not my ide haha
Angius
Angius2y ago
Ah, it's one of them fancy code screenshot things Well, FYI, $code
MODiX
MODiX2y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
garrus_j
garrus_jOP2y ago
a site that generates a "nice" looking code snippet haha ahh perfect
Odex
Odex2y ago
isn't LINQ a bit slower?
Angius
Angius2y ago
Yeah, tends to be Will the performance difference matter..? Up to you to decide Benchmark if need be
Tvde1
Tvde12y ago
var receivedTime = DateTimeOffset.UtcNow;
var values = price.Elements
.SelectMany(x => x.Timespans)
.Select(x => new PriceValue(receivedTime)
{
...
})
.ToList();
var receivedTime = DateTimeOffset.UtcNow;
var values = price.Elements
.SelectMany(x => x.Timespans)
.Select(x => new PriceValue(receivedTime)
{
...
})
.ToList();
if you had the code, I would have pasted the object initializer in it
ero
ero2y ago
PISSED using x as the Func parameter name!
Tvde1
Tvde12y ago
ah wait you need element inside the second select, hold on
var values = price.Elements
.SelectMany(
element => element.Timespans,
(element, timespan) =>
new PriceValue(receivedTime)
{
DataId = price.Key,
Unit = price.Currency,
Timestamp = MergeDateAndTimespan(element.Date, timeSpan.TimeSpan),
Value = timespan.Value,
})
.ToList();
var values = price.Elements
.SelectMany(
element => element.Timespans,
(element, timespan) =>
new PriceValue(receivedTime)
{
DataId = price.Key,
Unit = price.Currency,
Timestamp = MergeDateAndTimespan(element.Date, timeSpan.TimeSpan),
Value = timespan.Value,
})
.ToList();
SelectMany has an overload where the second parameter is a .Select() where you get both the 2nd collection item and the first
garrus_j
garrus_jOP2y ago
Thanks, it looks cleaner in LINQ to me. I don't think performance has much to say as it's a worker that runs a task a few times a day and just stores the prices
Tvde1
Tvde12y ago
it should be about 15 times slower, but you won't notice that
Angius
Angius2y ago
Doubt it would be that much slower. Benchmark if you want to know details
Tvde1
Tvde12y ago
https://github.com/Tvde1/RandomBenchmarks/tree/master/DigitParsing This benchmark showed LINQ was 12 times slower than the equivalent for loop and then there is some magic code that Tanner wrote
Jester
Jester2y ago
yeah use o instead
ero
ero2y ago
WhatChamp
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server