2

作为一个整体,我是 C# 的新手,想知道如何实现下面描述的功能。目前,这不是在指定的行编译。我想要代码做的是:

遍历每个KVP,使用keystring作为表名查询db 返回列表

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
        }
    }
4

1 回答 1

1

最接近我找到的答案,但这意味着我必须传入类型,而不是像上面想要的那样通过字典访问它:

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);
        }
    }

用法如:

 GetAndStoreIntKey<Sport>("Sport");
于 2013-07-11T04:45:59.230 回答