❔ IEnumerable to ObservableCollection

I am not sure if im doing this correctly but I am trying to create a sqlite table with some data in it and then load the data from the table. I havent tried to run the code yet because when I follow along with the tutorial the code he writes pulls the list as a IEnumerable but I have the list as an observable collection so that the UI will change when the list is updated. Is there a way to change IEnumerables to ObservableCollections? And is that even what I should do here? Thanks https://paste.mod.gg/cpttdpqbguai/0
BlazeBin - cpttdpqbguai
A tool for sharing your source code with the world!
27 Replies
Jimmacle
Jimmacle8mo ago
iterate over the IEnumerable and .Add each element to the observable collection?
Insire
Insire8mo ago
except that you dont want to load the data from the table asynchronously in the ctor of your viewmodel you dont do async in ctors because its not supported and if you do it anyway, you get fun bugs eventually and you'll only discover them eventually during runtime (or not at all and your users will instead)
EricAwesomeness
EricAwesomeness8mo ago
Where would the best place be then? Just create another function in the viewmodel that iterates like Jimmacle was suggesting and call THAT from the constructor? @Insire sorry I hope pinging you isn't a huge bother just not sure if you will see this if I don't
Insire
Insire8mo ago
that other method would need to be async aswell, and you'll be back where you started there is 2 ways to have async code in c# GUIs. async (void) eventhandlers and the entry point of the procss, aka the main method you usually dont have access to the main method which leaves eventhandlers
nohopestage
nohopestage8mo ago
You can also use async commands
Insire
Insire8mo ago
some1 still has to trigger them
EricAwesomeness
EricAwesomeness8mo ago
I am learning now about events which is a new topic so it's taking me a bit but this is pointing me in the right direction
nohopestage
nohopestage8mo ago
Yeah well you can do that manually
Insire
Insire8mo ago
yes, from an eventhandler
nohopestage
nohopestage8mo ago
Not necessarily, I usually do it from the factory method of my vms
EricAwesomeness
EricAwesomeness8mo ago
So if I'm understanding correctly the class that I load the sql data from should be setup as the publisher and create a delegate there. Then the subscriber classes uses the signature the delegate setup to subscribe to the event so when the event goes off, in this case the event is the player data loads, the subscribers event handler which is on the mainviewmodel can take the event args it gets and use it right?
Insire
Insire8mo ago
wha.. just subscribe to the loaded event of your window or something then start your async method there wrap it in a try catch, call it a day
EricAwesomeness
EricAwesomeness8mo ago
Oh wow. That's a simpler then creating my own event. And subscribing to that. Should the async method be on the data class or the mainviewmodel or does it not matter?
Insire
Insire8mo ago
the async method is what is doing async work sometimes thats the viewmodel sometimes thats a view(window or control)
nohopestage
nohopestage8mo ago
@EricAwesomeness I'd create an async command (you could use AsyncRelayCommand from CommunityToolkit.Mvvm), create a static method in the vm class that creates a new instance of it (the vm class), executes the command and returns the instance So the command would await LoadPlayers and assign the result to Players
Insire
Insire8mo ago
... and you'll still be calling that command from an eventhandler, like the loaded event i mentioned before, having a command is useful if you want to bind it to a button or something, otherwise its pointless busywork
nohopestage
nohopestage8mo ago
No, I meant calling it from the factory method
Insire
Insire8mo ago
i dont think eric has a factory method
nohopestage
nohopestage8mo ago
Yeah, I suggested creating it as an option Words are hard
EricAwesomeness
EricAwesomeness8mo ago
Sorry im back home and have time to work on this What is a factory method?
nohopestage
nohopestage8mo ago
It creates a new instance of MainViewModel, populates the collection by executing a command and returns the instance
Insire
Insire8mo ago
a method with the purpose of creating (calling a ctor) something
Accord
Accord8mo 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.
EricAwesomeness
EricAwesomeness8mo ago
Is this the better way to do it then? In the constructor of main view model i subscribe to the window creation event and then when the event triggers it creates the DataWarehouse class and runs a method from there that returns an observablecollection to the mainviewmodel? https://paste.mod.gg/llcexsgdgdph/0 Im sorry if im still not understanding quite well. But I believe this is what you meant right?
Insire
Insire8mo ago
you can't reference a window in your viewmodel, that breaks MVVM that means your viewmodel knows about the view which is the opposite you try to achieve with MVVM just put this
Activated += (s, e) =>
{
LoadData();
};
}
Activated += (s, e) =>
{
LoadData();
};
}
in your window
Accord
Accord8mo 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
❔ EF Core optional where extension methodI have a lot of optional parameters for queries. Filter by this, filter by that etc. I can do it lik❔ Need help on UI Design and tips for implementation - .Net Mauithree questions: 1. How can I edit the blue bar on the top? I'm not sure where its coming from, I o❔ Implementation of Search page in ASP. NET MVC along with stored procedureCan anyone help me to create a webform to search the data from the database to filter it on web page❔ MySQL server issue (Error 28)I'll prefix this with a few things: - I'm not driven with Ubuntu or other unix based systems. - The ✅ data override in dictionary of dictionariesI'm trying to create a dictionary of dictionaries, and use temp variable to create it, but when i chNeed help with homeworkHiya, I've been trying to solve an exercise for a bit but it's boggling me. The exercise is: Write ✅ Databinding using CommunityTooklit.MVVMWhen I am trying to bind to a property on a custom class I am running into some trouble because im n✅ Can anyone give example code of using an Immutable Data Structure for Concurrency?Can anyone give an example that shows how using these types are thread safe?❔ ✅ Databinding using CommunityToolkit.MVVMI am getting intellisense telling me "Member not found in Data context MainViewModel" on lines 23 an❔ Streama audiofile with capability to skip to a specific durationSo for startes, I am making a .net core webapi that uses youtubeAPI to search for music and uses you