❔ 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
I see the reason why it's ugly, your colour scheme 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?
Alright, LINQ here i come. Thanks!
it was the default on ray.so
tf is
ray.so
definitely not my ide haha
Ah, it's one of them fancy code screenshot things
Well, FYI, $code
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/a site that generates a "nice" looking code snippet haha
ahh perfect
isn't LINQ a bit slower?
Yeah, tends to be
Will the performance difference matter..? Up to you to decide
Benchmark if need be
if you had the code, I would have pasted the object initializer in it
using
x
as the Func parameter name!ah wait you need
element
inside the second select, hold on
SelectMany has an overload where the second parameter is a .Select()
where you get both the 2nd collection item and the firstThanks, 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
it should be about 15 times slower, but you won't notice that
Doubt it would be that much slower. Benchmark if you want to know details
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
yeah use
o
insteadWas 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.