Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何根据声明的类型检查c#字典中是否存在某一行_C#_.net_Dictionary - Fatal编程技术网

如何根据声明的类型检查c#字典中是否存在某一行

如何根据声明的类型检查c#字典中是否存在某一行,c#,.net,dictionary,C#,.net,Dictionary,假设我创建了一个列表,其成员中有一个字典 List<FieldValues> fieldValues = new List<FieldValues>() { new FieldValues(tableFields[0]) { dictionary = new Dictionary<int, string>() { {0, "" }, {1, "" }

假设我创建了一个列表,其成员中有一个字典

    List<FieldValues> fieldValues = new List<FieldValues>()
{
    new FieldValues(tableFields[0]) {
        dictionary = new Dictionary<int, string>()
        {
            {0, "" },
            {1, "" }
        }

    },
    new FieldValues(tableFields[1]) {
        dictionary = new Dictionary<int, string>()
        {
            {0, "x" },
            {1, "x" }
        }

    },
    new FieldValues(tableFields[2]) {
        dictionary = new Dictionary<int, string>()
        {
            {0, "x" },
            {1, "x" }
        }

    },
    new FieldValues(tableFields[3])  {
        dictionary = new Dictionary<int, string>()
        {
            {0, "Car" },
            {1, "Travel" }
        }

    },
};
List fieldValues=新列表()
{
新字段值(表字段[0]){
字典=新字典()
{
{0, "" },
{1, "" }
}
},
新字段值(tableFields[1]){
字典=新字典()
{
{0,“x”},
{1,“x”}
}
},
新字段值(tableFields[2]){
字典=新字典()
{
{0,“x”},
{1,“x”}
}
},
新字段值(tableFields[3]){
字典=新字典()
{
{0,“Car”},
{1,“旅行”}
}
},
};
请注意,我创建了类FieldValues:Fields,如下所示:

enum FieldType
{
    Boolean,
    Integer,
    String
}

class Fields
{
    public FieldType type { get; set; }
    public string name { get; set; }

    //declar fields(columns) name and types (types should be as in mysql syntax)
    public string GetFieldDeclaration()
    {
        switch (type)
        {
            case FieldType.Boolean:
                return name + " tinyint(1)";
            case FieldType.Integer:
                return name + " int";
            case FieldType.String:
                return name + " varchar(2048)";
            default:
                return string.Empty;
        }
    }
}

class FieldValues:Fields
{
    public Dictionary<int, string> dictionary { get; set; }

    //in order to add a new field value of the respected field
    public FieldValues(Fields field)
    {
        type = field.type;
        name = field.name;
        dictionary = new Dictionary<int, string>();
    }


    public void AddToDictionary(int row, string cellValue)                             
    {

        if (dictionary.ContainsKey(row))
        {
            dictionary[row] = cellValue; 
        }
        else
        {
            dictionary.Add(row, cellValue); //else adaug randul la tabela
        }
    }

    public string GetRowValue(int row)
    {
        if (dictionary.ContainsKey(row))
        {
            if()
        }
        return string.Empty;
    }


}
enum字段类型
{
布尔,
整数,
一串
}
类字段
{
公共字段类型类型{get;set;}
公共字符串名称{get;set;}
//declar字段(列)名称和类型(类型应与mysql语法相同)
公共字符串GetFieldDeclaration()
{
开关(类型)
{
大小写字段类型。布尔值:
返回名称+“tinyint(1)”;
大小写字段类型。整数:
返回name+“int”;
大小写字段类型。字符串:
返回名称+“varchar(2048)”;
违约:
返回字符串。空;
}
}
}
类字段值:字段
{
公共字典{get;set;}
//以添加受尊重字段的新字段值
公共字段值(字段)
{
type=field.type;
name=field.name;
字典=新字典();
}
public void AddToDictionary(int行,字符串cellValue)
{
if(字典.容器(行))
{
字典[行]=单元格值;
}
其他的
{
dictionary.Add(row,cellValue);//else adaug randul la tabela
}
}
公共字符串GetRowValue(int行)
{
if(字典.容器(行))
{
if()
}
返回字符串。空;
}
}
我将使用所有这些将excel电子表格值插入mysql数据库,如下所示:

string createQuery2 = createQuery + "\n INSERT INTO `poi_specs_mgu_ece_1.6` (";

        for (var i = 0; i < tableFields.Count; i++)
        {
            createQuery2 += "\n\t" + tableFields[i].GetFieldDeclaration() + (i == tableFields.Count - 1 ? string.Empty : ",");
        }
        createQuery2 += ")\n VALUES ( ";


        for (var i = 0; i < fieldValues.Count; i++)
        {

            createQuery2 += "\n\t" + fieldValues[i].dictionary[0] + fieldValues[i].dictionary[1];
        }
        createQuery2 += " \n);";
string createQuery2=createQuery+”\n插入'poi_specs_mgu_ece_1.6'(“;
对于(var i=0;i
我想在FieldValues类中创建一个方法GetRowValue(),该方法检查字典中是否存在某一行,根据这一点,我现在将讨论应该插入哪种类型的值,似乎我一直在关注这一点


任何帮助都将不胜感激。谢谢大家!

嗯,你只是在找
返回字典[row]
?这肯定是我见过的最令人费解的家庭作业了。我想检查一下我应该在每个字段的某一行插入什么类型的作业。“字段”被解释为excel列,其中列可以是不同的类型:int、boolean(tinyint(1))或string。我想检查字典是否有该行,如果有,我想确定可以在每个字段中插入哪种类型的值
public string GetRowValue(int row)
{
    string result;
    if(!dictionary.TryGetValue(row, out result))
        result = "";
    return result;
}