C
C#8mo 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
NinjaOla8mo ago
paste this question into chatgpt. and i think u will be happy with your answer also bonus: ask it to explain
Pobiega
Pobiega8mo 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
Livid8mo ago
i mean might there be a performance issue if List<Animal> is quite big
Pobiega
Pobiega8mo ago
sure, but then you shouldn't be having a list in the first place
Livid
Livid8mo ago
but then i won't be improving my explanation skills😔
Thinker
Thinker8mo ago
don't do that lmao
NinjaOla
NinjaOla8mo ago
Now i wonder eiter my skills are bad or i got something else from it than you?
Thinker
Thinker8mo ago
wat
Livid
Livid8mo ago
wat
Pobiega
Pobiega8mo ago
wat
NinjaOla
NinjaOla8mo ago
Ask chatgpt for an explaination and solution
Thinker
Thinker8mo 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
NinjaOla8mo ago
Ah then i understand
Thinker
Thinker8mo ago
they asked a question here
NinjaOla
NinjaOla8mo ago
I think its a tool that is often not looked at, so
Thinker
Thinker8mo ago
it's looked at too much lmao
Livid
Livid8mo ago
i know how to do it however
Pobiega
Pobiega8mo ago
Don't.
Livid
Livid8mo ago
i was asking if there is a better way than looping
Pobiega
Pobiega8mo ago
Plenty
Livid
Livid8mo ago
gimme
Pobiega
Pobiega8mo 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
Livid8mo ago
wat
Pobiega
Pobiega8mo ago
what part of that question made you go wat?
Livid
Livid8mo ago
wat
Pobiega
Pobiega8mo ago
so I know what to explain
Livid
Livid8mo ago
collection type?
Pobiega
Pobiega8mo ago
List<T> is a collection type T[] (array) is also a collection type can you name some more?
Livid
Livid8mo ago
why T?
Pobiega
Pobiega8mo ago
its just a placeholder for the actual type
Livid
Livid8mo ago
oh
Pobiega
Pobiega8mo ago
by convention we use the letter T for that
Livid
Livid8mo ago
hashmap
Pobiega
Pobiega8mo ago
Yep!
Livid
Livid8mo ago
dictionary
Pobiega
Pobiega8mo ago
yep! called HashSet in C#, but its the same
Livid
Livid8mo ago
oh yes
Pobiega
Pobiega8mo ago
so what do hashsets and dictionaries have as their "special" property?
Livid
Livid8mo ago
no duplicate?
Pobiega
Pobiega8mo 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
Livid8mo ago
so you can set id as the key for dictionaries?
Pobiega
Pobiega8mo ago
that sounds like an excellent idea
Livid
Livid8mo ago
but i still have to loop to convert from list to dictionary
Pobiega
Pobiega8mo ago
for one, it makes it O(1) to look if an ID already exists
Livid
Livid8mo ago
oh wait i don't know if i can
Pobiega
Pobiega8mo ago
instead of O(N)
Livid
Livid8mo ago
since i'm using a winform datasource
Pobiega
Pobiega8mo ago
okay You're a beginner, right?
Livid
Livid8mo ago
sorta yes
Pobiega
Pobiega8mo ago
Don't think you need to worry about performance of filtered list inserts etc just yet 🙂
Livid
Livid8mo 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