Reputation: 31
I am new to C# as a whole and was wondering how I would achieve the functionality described below. Currently this is not compiling at the indicated lines. What I would like the code to do is:
Iterate through each KVP, Query the db using the keystring as the table name Return a list
var dbCon = dbConnectionFactory.OpenDbConnection();
Dictionary<string, Type> ibetDic = getFromSomeWhere();
foreach (KeyValuePair<string, Type> entry in ibetDic)
{
Type type = entry.Value;
var typedRedisClient = redis.GetTypedClient<type>();/*not compiling here*/
String sql = "USE ibet SELECT * FROM " + entry.Key;
var itemList = dbCon.SqlList<type>(sql);/*not compiling here*/
foreach (var tableRow in itemList )
{
//store with redistypedclient
}
}
Upvotes: 2
Views: 670
Reputation: 31
Closest thing to an answer I have found however this means that I have to pass in the type rather than be able to access it through the dictionary as I was wanting above:
public void GetAndStoreIntKey<T>(string tableName) where T : IHasId<int>
{
var dbCon = dbConnectionFactory.OpenDbConnection();
String sql = "USE ibet SELECT * FROM " + tableName;
var items = dbCon.SqlList<T>(sql);
var typedRedisClient = redis.As<T>();
foreach (T item in items)
{
typedRedisClient.SetEntry(UrnId.CreateWithParts<T>(new string[] {item.Id + ""}), item);
}
}
Usage like :
GetAndStoreIntKey<Sport>("Sport");
Upvotes: 1