C# 存储列数据配置CSV文件以供以后搜索和使用的更好方法

C# 存储列数据配置CSV文件以供以后搜索和使用的更好方法,c#,C#,我的程序有一个CSV配置文件,我在启动时读取该文件,并将其存储到各种阵列中供以后使用。它包含两列(目前)成对数据,如下所示: 1,10 2,14 3,11 蓝色,4-10202 红色,4-10001 在此文件片段中,前三行仅使用一次,我执行以下操作: int[][] BayPortMap = new int[10][]; for (int i = 0; i < 10; i++) { BayPortMap[i] = new int[3]; } var contents = Fil

我的程序有一个CSV配置文件,我在启动时读取该文件,并将其存储到各种阵列中供以后使用。它包含两列(目前)成对数据,如下所示:

1,10
2,14
3,11
蓝色,4-10202
红色,4-10001

在此文件片段中,前三行仅使用一次,我执行以下操作:

int[][] BayPortMap = new int[10][]; 

for (int i = 0; i < 10; i++)
{
   BayPortMap[i] = new int[3];
}

var contents = File.ReadAllLines(@"C:\CSI\MotorTesterConfig.csv");
contents = contents.Where(x =>!string.IsNullOrEmpty(x.Trim(','))).ToArray();
var csv = from line in contents select line.Split(',');
int HeaderRows = 1;
foreach (var row in csv.Skip(HeaderRows))
{
   if (csvindex < 10)
   {
       BayPortMap[csvindex][0] = Convert.ToInt32(row[0]);
       BayPortMap[csvindex][1] = Convert.ToInt32(row[1]);
       BayPortMap[csvindex][2] = -1;
   }
}
它会工作,但感觉笨重。我稍后将编写的另一个程序有许多列“数据”,需要根据匹配的PN使用这些数据


那么,我应该继续这样做,还是使用其他方法,如列表等。

最终使用了字典

Dictionary<string, string> MotorPNDict = new Dictionary<string, string>();

        int csvindex = 0;
        int HeaderRows = 1;
        foreach (var row in csv.Skip(HeaderRows))
        {
           if (csvindex < 10)
           {
              /* Here we get the Bay/Port mapping and store them in ascending order regardless of the CSV order, i.e. bay 1 will always be array position 0*/
              var index = Convert.ToInt32(row[0]) - 1;
              BayPortMap[index][0] = Convert.ToInt32(row[0]);
              BayPortMap[index][1] = Convert.ToInt32(row[1]);
              BayPortMap[index][2] = -1;
           }
           else
           {
              switch (csvindex)
              {
                 case 10:
                    {
                       TesterNumber = row[1];
                       break;
                    }
                 case 11:
                    {
                       ReportLocation = row[1];
                       break;
                    }
                 case 12:
                    {
                       DBSource = row[1];
                       break;
                    }
                 case 13:
                    {
                       InitialCatalog = row[1];
                       break;
                    }
                 default:
                    {
                       /* add Motor PNs and there corresponding types to a dictionary */
                       MotorPNDict.Add(row[1], row[0]);
                       break;
                    }
              }
           }
           csvindex++;
        }
Dictionary-MotorPNDict=new Dictionary();
int-csvindex=0;
int HeaderRows=1;
foreach(csv.Skip中的变量行(HeaderRows))
{
如果(csvindex<10)
{
/*在这里,我们获得了间隔/端口映射,并以升序存储它们,而不管CSV顺序如何,即间隔1始终是数组位置0*/
var index=Convert.ToInt32(行[0])-1;
BayPortMap[index][0]=Convert.ToInt32(行[0]);
BayPortMap[index][1]=Convert.ToInt32(行[1]);
BayPortMap[索引][2]=-1;
}
其他的
{
开关(csvindex)
{
案例10:
{
TesterNumber=行[1];
打破
}
案例11:
{
ReportLocation=行[1];
打破
}
案例12:
{
DBSource=行[1];
打破
}
案例13:
{
InitialCatalog=行[1];
打破
}
违约:
{
/*将电机PNs和相应的类型添加到字典中*/
MotorPNDict.Add(第[1]行,第[0]行);
打破
}
}
}
csvindex++;
}

< /代码>您可以考虑使用数据库。创建一个支持您需要的数据并将其序列化到磁盘的类,例如参见。或者使用一个(NoSQL)db,比如我应该只搜索CSV文件,然后以这种方式获取数据吗?这也不是最好的,把它拉到内存中的某种类型,我可以工作似乎更好??我应该补充一点,CSV文件需要(轻松地)最终用户可更新,所以没有数据库。我做了更多的搜索,我想我要的是一个KeyValuePair和更多的数据列,一个元组。想法还是例子?
Dictionary<string, string> MotorPNDict = new Dictionary<string, string>();

        int csvindex = 0;
        int HeaderRows = 1;
        foreach (var row in csv.Skip(HeaderRows))
        {
           if (csvindex < 10)
           {
              /* Here we get the Bay/Port mapping and store them in ascending order regardless of the CSV order, i.e. bay 1 will always be array position 0*/
              var index = Convert.ToInt32(row[0]) - 1;
              BayPortMap[index][0] = Convert.ToInt32(row[0]);
              BayPortMap[index][1] = Convert.ToInt32(row[1]);
              BayPortMap[index][2] = -1;
           }
           else
           {
              switch (csvindex)
              {
                 case 10:
                    {
                       TesterNumber = row[1];
                       break;
                    }
                 case 11:
                    {
                       ReportLocation = row[1];
                       break;
                    }
                 case 12:
                    {
                       DBSource = row[1];
                       break;
                    }
                 case 13:
                    {
                       InitialCatalog = row[1];
                       break;
                    }
                 default:
                    {
                       /* add Motor PNs and there corresponding types to a dictionary */
                       MotorPNDict.Add(row[1], row[0]);
                       break;
                    }
              }
           }
           csvindex++;
        }