C
C#β€’13mo ago
Salty

❔ Iteration over multiple objects at the same time

How do I iterate through several objects at the same time. Supposedly the objects have the same properties and I want to concat obj1.prop1 + obj2.prop1, obj1.prop2+obj2.prop2 etc etc
21 Replies
Pobiega
Pobiegaβ€’13mo ago
.Zip list1.Zip(list2).Select(...) should do roughly that
Salty
Saltyβ€’13mo ago
what if i have 100 lists
Pobiega
Pobiegaβ€’13mo ago
use a for loop also, why do you have 100 lists πŸ˜„
Salty
Saltyβ€’13mo ago
well its 100 objects, each is sorta like a data set image them as replicates of an experiment i want to stitch same properties from each object together
Pobiega
Pobiegaβ€’13mo ago
right. I'd personally do that by putting all your objects into an array, then use Aggregate on that array Its fine thinker, you dont have to delete πŸ˜„
Salty
Saltyβ€’13mo ago
aggregate, alright ill look into it wheres my chatgpt...
Thinker
Thinkerβ€’13mo ago
check the docs
Pobiega
Pobiegaβ€’13mo ago
Enumerable.Aggregate Method (System.Linq)
Applies an accumulator function over a sequence. The specified seed value is used as the initial accumulator value, and the specified function is used to select the result value.
Pobiega
Pobiegaβ€’13mo ago
check the examples here
Thinker
Thinkerβ€’13mo ago
Essentially
lists.Aggregate((a, b) =>
new Thing(
a.A + b.A,
a.B + b.B,
/*etc.*/));
lists.Aggregate((a, b) =>
new Thing(
a.A + b.A,
a.B + b.B,
/*etc.*/));
Pobiega
Pobiegaβ€’13mo ago
the actual method definition is hard to read
Salty
Saltyβ€’13mo ago
ill take a look
Topend
Topendβ€’13mo ago
this is correct, Thing() can be object or function, and this will save on total ops
Thinker
Thinkerβ€’13mo ago
?
Salty
Saltyβ€’13mo ago
what is "lists" list of lists?
Pobiega
Pobiegaβ€’13mo ago
no its the list of things your "data sets"
Salty
Saltyβ€’13mo ago
i have a list of x objects or just x objects
Pobiega
Pobiegaβ€’13mo ago
you'll need them to be enumerable, so you need to stick them in an array or something
Salty
Saltyβ€’13mo ago
thats not a problem
Pobiega
Pobiegaβ€’13mo ago
example:
var datasets = Enumerable.Range(0, 100).Select(_ => Dataset.Create()).ToArray();

var aggregateDataset = datasets.Aggregate((a, b) =>
{
a.A += b.A;
a.B += b.B;
a.C += b.C;
return a;
});

Console.WriteLine(aggregateDataset);
var datasets = Enumerable.Range(0, 100).Select(_ => Dataset.Create()).ToArray();

var aggregateDataset = datasets.Aggregate((a, b) =>
{
a.A += b.A;
a.B += b.B;
a.C += b.C;
return a;
});

Console.WriteLine(aggregateDataset);
the top line just creates 100 randomized "datasets"
Accord
Accordβ€’13mo 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
More Posts
❔ accessing collections via principal entity vs directly on dbsetwhat is generally preferred if i want the accommodations of a listing (WITH resource ownership checkβœ… How to deploy gRPC Server + Client via dockerI wish to have a gRPC server and multiple clients connected to said server. In my current setup, I h❔ How to "mute and resume" in RiderWhen I have an exception breakpoint for all CLR exceptions, when the debugger halts for an exception❔ Best way to set up a websocket and API to access the data?I'm working on a website that gathers data through a websocket on the backend, using raw UDP packets❔ Blazor WebassemblyTrying to put an image into a canvas so I can use coordinates to place images in different spots. Tr❔ Log into minecraft with xbox/microsoft apiI was wondering how to authenticate a Minecraft account with Microsofts new account system and if th❔ optimizationSincere question about optimization here... The RenderFirstTab() method is called in an infinite lo❔ How to instantiate derived class when base class requires args?Hey, I'll post the code below but I don't think it'll be necessary as I'm pretty sure I know what th❔ Not sure should I leave the automatic table alteration (postgres EF Core 3.0 to EF Core 6.0)You can see on the picture that EF Core has added some non intentional AlterColumn entries. I guess ❔ Writing VS Extension to Support Language with TextMate Grammar FileHello, I am trying to write extension to support BNF(Backus–Naur form) metalanguage files. I am try