【发布时间】:2017-07-25 20:05:02
【问题描述】:
我的Program.cs 文件中有一个我正在尝试实现的方法。当该方法遍历作业列表(其中每个作业都是字典)时,它应该打印:
*****
name: Data Scientist / Business Intelligence
employer: Bull Moose Industries
location: Saint Louis
position type: Sr. IT Analyst (Data/BI)
core competency: Statistical Analysis
*****
文件job_data.csv 包含我需要的所有工作信息:
name,employer,location,position type,core competency
Junior Data Analyst,Lockerdome,Saint Louis,Data Scientist / Business Intelligence,Statistical Analysis
Project Coordinator Support,Maritz,Saint Louis,Technical Assistant / User Support,Non-coding
Junior Web Developer,Cozy,Portland,Web - Front End,Ruby
Junior Developer 3,LiveAnswer,South Florida,Web - Full Stack,Java
Full Stack Engineer,Splitwise,Rhode Island,Web - Full Stack,Ruby
Customer Experience,Splitwise,Rhode Island,Project Manager / Analyst,Non-coding
IT Support Specialist,Viamontech,South Florida,Technical Assistant / User Support,Non-coding
C#/.net Developer ,Hunter Engineering,Saint Louis,Software / Enterprise Developer,.Net
Junior Developer,"TruckMovers.com, Inc.",Kansas City,Web - Full Stack,Python
...等等(总共有 99 行)。我需要做的是编写PrintJobs 方法,以便它到达job_data.csv 并打印出标签:
name:
employer:
location:
position type:
core competency:
之后是每行中它们对应的项目。例如,打印第 10 行将如下所示:
*****
name: Junior Developer
employer: "TruckMovers.com, Inc."
location: Kansas City
positon type: Web - Full Stack
core competency: Python
*****
我该怎么做?该方法的起始代码是:
private static void PrintJobs(List<Dictionary<string, string>> someJobs)
{
Console.WriteLine("printJobs is not implemented yet");
}
我不完全确定参数someJobs 的来源。这也是我得到的提示:
为此,您需要遍历作业列表。每个工作本身就是一个字典。虽然您可以使用已知键(“雇主”、“位置”等)从字典中获取每个项目,但请考虑创建一个嵌套循环来循环遍历每个字典。您需要使用 Dictionary.Keys 属性来执行此操作。如果将新字段添加到作业记录中,则此方法将打印出新字段,而不对 PrintJobs 进行任何更新。
根据提示,我猜我需要执行以下操作:
private static void PrintJobs(List<Dictionary<string, string>> someJobs)
{
//match up index value with label, write label + value
if (i = [0])
{Console.WriteLine("name: "); + string someJobs}
else if (i = [1])
{Console.WriteLine("employer: "); + string someJobs}
else if (i = [2])
{Console.WriteLine("location: "); + string someJobs}
else if (i = [3])
{Console.WriteLine("positon type: "); + string someJobs}
else if (i = [4])
{Console.WriteLine("core competencey: "); + string someJobs}
}
我试过了,但仍然收到消息:
printJobs is not implemented yet
完全是 C# 初学者。
完整代码链接:
【问题讨论】:
标签: c# csv dictionary