C
C#14mo ago
Livid

✅ Comparing properties of a values of lists

i have 2 lists List<Animal> and List<Dog> and both Animal and Dog have the property id i'm trying to insert a list of Dog into the list of Animal but i don't want to insert Animals with duplicate id's
51 Replies
NinjaOla
NinjaOla14mo ago
paste this question into chatgpt. and i think u will be happy with your answer also bonus: ask it to explain
Pobiega
Pobiega14mo ago
List<Dog> dogs = new List<Dog>();
List<Animal> animals = new List<Animal>();

List<Dog> dogsNotInAnimals = ...;

animals.AddRange(dogsNotInAnimals);
List<Dog> dogs = new List<Dog>();
List<Animal> animals = new List<Animal>();

List<Dog> dogsNotInAnimals = ...;

animals.AddRange(dogsNotInAnimals);
can you think of a way to calculate dogsNotInAnimals?
Livid
LividOP14mo ago
i mean might there be a performance issue if List<Animal> is quite big
Pobiega
Pobiega14mo ago
sure, but then you shouldn't be having a list in the first place
Livid
LividOP14mo ago
but then i won't be improving my explanation skills😔
Thinker
Thinker14mo ago
don't do that lmao
NinjaOla
NinjaOla14mo ago
Now i wonder eiter my skills are bad or i got something else from it than you?
Thinker
Thinker14mo ago
wat
Livid
LividOP14mo ago
wat
Pobiega
Pobiega14mo ago
wat
NinjaOla
NinjaOla14mo ago
Ask chatgpt for an explaination and solution
Thinker
Thinker14mo ago
If you're gonna tell someone to just go ask a text prediction engine then why even respond in the first place? This person asked a question, you gave them a non-answer If they wanted to use chatgpt then they would've done that
NinjaOla
NinjaOla14mo ago
Ah then i understand
Thinker
Thinker14mo ago
they asked a question here
NinjaOla
NinjaOla14mo ago
I think its a tool that is often not looked at, so
Thinker
Thinker14mo ago
it's looked at too much lmao
Livid
LividOP14mo ago
i know how to do it however
Pobiega
Pobiega14mo ago
Don't.
Livid
LividOP14mo ago
i was asking if there is a better way than looping
Pobiega
Pobiega14mo ago
Plenty
Livid
LividOP14mo ago
gimme
Pobiega
Pobiega14mo ago
Assuming we can change some types around if you are working with two lists, there isnt much we can do Can you think of a collection type that could be used to help with the "unique id" requirement?
Livid
LividOP14mo ago
wat
Pobiega
Pobiega14mo ago
what part of that question made you go wat?
Livid
LividOP14mo ago
wat
Pobiega
Pobiega14mo ago
so I know what to explain
Livid
LividOP14mo ago
collection type?
Pobiega
Pobiega14mo ago
List<T> is a collection type T[] (array) is also a collection type can you name some more?
Livid
LividOP14mo ago
why T?
Pobiega
Pobiega14mo ago
its just a placeholder for the actual type
Livid
LividOP14mo ago
oh
Pobiega
Pobiega14mo ago
by convention we use the letter T for that
Livid
LividOP14mo ago
hashmap
Pobiega
Pobiega14mo ago
Yep!
Livid
LividOP14mo ago
dictionary
Pobiega
Pobiega14mo ago
yep! called HashSet in C#, but its the same
Livid
LividOP14mo ago
oh yes
Pobiega
Pobiega14mo ago
so what do hashsets and dictionaries have as their "special" property?
Livid
LividOP14mo ago
no duplicate?
Pobiega
Pobiega14mo ago
thats hashsets, yes dictionaries are keyed meaning you set a key for each value, and you can only have one item per key do one of these sound good for storing a list of items with unique IDs?
Livid
LividOP14mo ago
so you can set id as the key for dictionaries?
Pobiega
Pobiega14mo ago
that sounds like an excellent idea
Livid
LividOP14mo ago
but i still have to loop to convert from list to dictionary
Pobiega
Pobiega14mo ago
for one, it makes it O(1) to look if an ID already exists
Livid
LividOP14mo ago
oh wait i don't know if i can
Pobiega
Pobiega14mo ago
instead of O(N)
Livid
LividOP14mo ago
since i'm using a winform datasource
Pobiega
Pobiega14mo ago
okay You're a beginner, right?
Livid
LividOP14mo ago
sorta yes
Pobiega
Pobiega14mo ago
Don't think you need to worry about performance of filtered list inserts etc just yet 🙂
Livid
LividOP14mo ago
i tried using a directory / bindingsource
BindingSource bs = new BindingSource()
{
DataSource = _artikelsDic.Values.ToList(),

};
BindingSource bs = new BindingSource()
{
DataSource = _artikelsDic.Values.ToList(),

};
but it doesn't update correctly how should i do it?
BindingSource bs = new BindingSource()
{
DataSource = _artikelsList,
DataMember = "value"

};
BindingSource bs = new BindingSource()
{
DataSource = _artikelsList,
DataMember = "value"

};
this gives me only one value? dead chat
Want results from more Discord servers?
Add your server