0

I am new to C# and have learned the basics now I am trying to create crud application using Dictionary since it is better than list<t> because of hash algorithm lookup.

I wanted the following using 'Dictionary< List<T>,List<T> >' or Dictionary<key,List<property_classname>>

  • Insert values to database using dictionary.
  • Update operation to database using dictionary.
  • Delete operation to database using dictionary.
  • Read operation to database using dictionary.

Please suggest me a tutorial for it.I searched it but didn't find even msdn MSDN For Dictionary doesn't satisfy my learning requirement

Apart from that I am working on a login page using dictionary as following: I have two list<T> filling from database successfully:

List<A> string type for all users details who are allowed to login, List<B> string type contains the database of ip addresses which are blocked.

I wanted to use the dictionary< List<A>,List<B>> in such a way that first

  1. It lookup at List<A> and selects the user with password.
  2. It lookup at the List<B> and checks if the current ip address of user is blocked or not.

For above purpose I don't get idea how to enumerate between these two 'List' I believe it is possible msdn definition : public class Dictionary<TKey, TValue> : IDictionary<TKey, TValue> but don't know how to do it?

Aman
  • 119
  • 7
  • The msdn link is msdn.microsoft.com/en-us/library/vstudio/xfhwa508%28v=vs.100%29.aspx – Aman Apr 14 '15 at 12:07
  • Have you filled the lists successfully ? – Mairaj Ahmad Apr 14 '15 at 12:13
  • 11
    You're doing something totally wrong. There is usually no point to have a dictionary with a key of type List<>. If you want a list of users, then you'll have a list of users. Having usernames in a list that is a key in Dictionary? Can't think of any use for that. So I assume you don't really know what Dictionary is. It's not "better than list because of hash algorithm lookup", it's *different* and is to be used in places where you need it. Not as a substitute for a List. – Sami Kuhmonen Apr 14 '15 at 12:13
  • @MairajAhmad yes I have filled list successfully – Aman Apr 14 '15 at 12:15
  • @SamiKuhmonen you may be correct but I am doing this for learning purpose since msdn suggest public class Dictionary : IDictionary so I just try to achieve the same means I can use generic key and generic value for dictionary so why not list – Aman Apr 14 '15 at 12:17
  • @Aman You *can*, but there is no *reason* to. A dictionary is a key-value store. Your list of usernames would be a key. Your list of IP addresses would be a value. Your dictionary therefore has only *one* entry, so looking it up is easy. But it won't allow you to look *in the lists* in any way. They are still lists. Keys and values also have a relation to each other. Here they don't. – Sami Kuhmonen Apr 14 '15 at 12:19
  • @SamiKuhmonen think that for a particular index of a list I have UserId and as key and for the other list I have details of ip address which used by that userid – Aman Apr 14 '15 at 12:22
  • @Aman Then you would use `Dictionary>`, if you want to have the IP addresses as strings (I would suggest otherwise). Then you add each user as a key and a separate list for the user as the IP addresses. This would be a correct way to use Dictionary. – Sami Kuhmonen Apr 14 '15 at 12:23
  • @Aman - the design you're suggestion doesn't make any sense. `Dictionary` should hold many keys of type `K`, not just one, that would be meaningless. If you want to learn C# you should think in classes: Have a `List`. A `User` may have properties like `string UserName {get;set;}` and `List BlockIps {get;set;}`. Now ask how to populate that data structure. Also, in practice, you only need to load *one* `User` for the login page, not the list of *all* users. – Kobi Apr 14 '15 at 12:26
  • @SamiKuhmonen I am agree with you but still i love c# for reusability and generic type so I wanted Dictionary < string , List> is it will be good idea? – Aman Apr 14 '15 at 12:26
  • @Aman Yes, using a `List<>`as a value in a dictionary is quite ok in places where you need to have lists for some key. Nothing wrong with that. – Sami Kuhmonen Apr 14 '15 at 12:29
  • @Kobi you are correct i have edited the question – Aman Apr 14 '15 at 12:32
  • please read this text, it could improve your skill: http://stackoverflow.com/questions/1247442/when-should-i-use-the-hashsett-type – Marek Woźniak Apr 14 '15 at 12:32
  • @Kobi should I remove the later part of question I was assuming that for Dictionary< K,V> K will contain only singular values like userid of user. – Aman Apr 14 '15 at 12:33
  • @W92 thanks I will surely go for it but please also provide me link for crud operations using dictionary collections. – Aman Apr 14 '15 at 12:35
  • if you have iterate throug your collection you would use list or ICollection, if you want to use it for create LINQ query - please use IQuerable (but List implements it). Dictionary can give you a way to fetch data from collection not neccessary to listen through full. Read any information about binary trees, hashset. Read an documentation of List and interfaces – Marek Woźniak Apr 14 '15 at 13:22

0 Answers0