Click here to Skip to main content
15,847,077 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
I have a record set returned from the DB

this recordset is used for 5 different transactions, I know the transaction Type here.

so I need to create 5 different Objects from this record set as each of 5 transactions are of different type.

Then I call the process method of each transaction and pass the resceptive object to the transation.

I have two problmes in this approach.

1. from the recordset returned by DB, I want to create a collection with Key Value pair, where Key is DB Column name and value is DB Column value and store it in a Dictionary, in Dictionary key is Transaction Type and value is my collection, I dont know how can I achieve that?

2. I want to write a generic method, to which I will pass the Dictionary created in step 1 and transaction type and that method should return me the object specific to the Transaction type.

I am very new to Generics, so can someone please help me in doing that.

Thanks in advance.

See the MSDN topic about Generic Methods.
Share this answer
Create a new dictionary:

var lookup = new Dictionary<string,>();

add your key and value to the dictionary

lookup.Add(DB Key Value, DB collection);

var mainDictionary = new Dictionary<Type, Dictionary<string, , object>>();

get the right collection:

public Dictionary<string, object> GetCollection<T>(Dictionary<Type, Dictionary<string, object>> mainDictionary)
    return mainDictionary.Where(x => x.Key == typeof(T)).Select(x => x.Value).SingleOrDefault();

NB this is Psuedo code, so no prizes for pointing out it won't compile ;-)
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900