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

Did you find this page helpful?