C# 将文本文件转换为对象字典
我有以下课程:C# 将文本文件转换为对象字典,c#,dictionary,C#,Dictionary,我有以下课程: class Car { public Make{get; set;} public Model{get; set;} public Year{get; set;} public Color{get; set;} public Car(string make, string model, string year, string color) { this.Make= make; this.Model
class Car
{
public Make{get; set;}
public Model{get; set;}
public Year{get; set;}
public Color{get; set;}
public Car(string make, string model, string year, string color)
{
this.Make= make;
this.Model= model;
this.Year= year;
this.Color= color;
}
}
我有以下文本文件“Carlist.txt”:
我想要一本以下形式的词典:
Dictionary<string, Car>
File.ReadLines
返回字符串数组
。然后,split
每个字符串将为您提供另一个string
数组string
不是int
,因此必须对其进行解析。还有你的第二个lambda被搞砸了。比如:
Dictionary<string, Car> result =
File.ReadLines("Carlist.txt")
.Select(line => line.Split(','))
.ToDictionary(split => int.Parse(split[0]),
split => new Car(split[1],
split[2],
split[3],
split[4]));
字典结果=
File.ReadLines(“Carlist.txt”)
.Select(line=>line.Split(','))
.ToDictionary(split=>int.Parse(split[0]),
split=>新车(split[1],
拆分[2],
拆分[3],
拆分[4]);
需要注意的是,如果第一个元素不能被解析为整数(如果您的文件确实包含这些头,那么它就不能),那么这将失败。因此,您需要跳过第一行和/或添加一些错误处理。
文件。ReadLines
返回一个字符串数组。然后,split
每个字符串将为您提供另一个string
数组string
不是int
,因此必须对其进行解析。还有你的第二个lambda被搞砸了。比如:
Dictionary<string, Car> result =
File.ReadLines("Carlist.txt")
.Select(line => line.Split(','))
.ToDictionary(split => int.Parse(split[0]),
split => new Car(split[1],
split[2],
split[3],
split[4]));
字典结果=
File.ReadLines(“Carlist.txt”)
.Select(line=>line.Split(','))
.ToDictionary(split=>int.Parse(split[0]),
split=>新车(split[1],
拆分[2],
拆分[3],
拆分[4]);
需要注意的是,如果第一个元素不能被解析为整数(如果您的文件确实包含这些头,那么它就不能),那么这将失败。因此,您需要跳过第一行和/或添加一些错误处理。有几个问题需要解决
首先,ToDictionary方法的每个参数都是一个委托,其语法如下:
.ToDictionary(split => int.Parse(split[0]),
split => new Car(split[1], split[2], split[3], split[4]));
而不是试图将委托传递给Car构造函数上的每个参数(如原始代码中所示)
第二个是,您将读取标题行并创建一辆以标题作为值的汽车,您将希望排除此项,一种方法是将此项添加到您的ToDictionary之上:
.Where( split => split[0] != "Id" )
这里有一个版本,可以满足你的需要
var result = File.ReadLines("Carlist.txt")
.Select(line => line.Split(','))
.Where( split => split[0] != "Id" )
.ToDictionary(split => int.Parse(split[0]), split => new Car(split[1], split[2], split[3], split[4]));
有几个问题你需要解决
首先,ToDictionary方法的每个参数都是一个委托,其语法如下:
.ToDictionary(split => int.Parse(split[0]),
split => new Car(split[1], split[2], split[3], split[4]));
而不是试图将委托传递给Car构造函数上的每个参数(如原始代码中所示)
第二个是,您将读取标题行并创建一辆以标题作为值的汽车,您将希望排除此项,一种方法是将此项添加到您的ToDictionary之上:
.Where( split => split[0] != "Id" )
这里有一个版本,可以满足你的需要
var result = File.ReadLines("Carlist.txt")
.Select(line => line.Split(','))
.Where( split => split[0] != "Id" )
.ToDictionary(split => int.Parse(split[0]), split => new Car(split[1], split[2], split[3], split[4]));
对于来这里寻找CSV反序列化的其他人:
在coma上读取CSV和拆分的方法适用于许多场景,但在许多场景中也会失败。例如,包含coma的CSV字段、带引号的字段或带转义引号的字段。这些都是非常常见的有效CSV字段,例如Excel
使用完全支持CSV的库既简单又兼容。一个这样的图书馆就是。如果需要手动控制,它支持多种映射,但在op描述的情况下,它非常简单:
public class Car
{
public string Make { get; set;}
public string Model { get; set; }
public int Year { get; set; }
public string Color { get; set; }
}
void Main()
{
List<Car> cars;
using (var fileReader = File.OpenText("Cars.txt"))
{
using (var csv = new CsvHelper.CsvReader(fileReader))
{
cars = csv.GetRecords<Car>().ToList();
}
}
// cars now contains a list of Car-objects read from CSV.
// Header fields (first line of CSV) has been automatically matched to property names.
// Set up the dictionary. Note that the key must be unique.
var carDict = cars.ToDictionary(c => c.Make);
}
公车
{
公共字符串Make{get;set;}
公共字符串模型{get;set;}
公共整数年{get;set;}
公共字符串颜色{get;set;}
}
void Main()
{
列出车辆清单;
使用(var fileReader=File.OpenText(“Cars.txt”))
{
使用(var csv=new CsvHelper.CsvReader(文件阅读器))
{
cars=csv.GetRecords().ToList();
}
}
//cars现在包含从CSV读取的Car对象列表。
//标题字段(CSV的第一行)已自动与属性名称匹配。
//设置字典。请注意,密钥必须是唯一的。
var carDict=cars.ToDictionary(c=>c.Make);
}
对于来这里寻找CSV反序列化的其他人:
在coma上读取CSV和拆分的方法适用于许多场景,但在许多场景中也会失败。例如,包含coma的CSV字段、带引号的字段或带转义引号的字段。这些都是非常常见的有效CSV字段,例如Excel
使用完全支持CSV的库既简单又兼容。一个这样的图书馆就是。如果需要手动控制,它支持多种映射,但在op描述的情况下,它非常简单:
public class Car
{
public string Make { get; set;}
public string Model { get; set; }
public int Year { get; set; }
public string Color { get; set; }
}
void Main()
{
List<Car> cars;
using (var fileReader = File.OpenText("Cars.txt"))
{
using (var csv = new CsvHelper.CsvReader(fileReader))
{
cars = csv.GetRecords<Car>().ToList();
}
}
// cars now contains a list of Car-objects read from CSV.
// Header fields (first line of CSV) has been automatically matched to property names.
// Set up the dictionary. Note that the key must be unique.
var carDict = cars.ToDictionary(c => c.Make);
}
公车
{
公共字符串Make{get;set;}
公共字符串模型{get;set;}
公共整数年{get;set;}
公共字符串颜色{get;set;}
}
void Main()
{
列出车辆清单;
使用(var fileReader=File.OpenText(“Cars.txt”))
{
使用(var csv=new CsvHelper.CsvReader(文件阅读器))
{
cars=csv.GetRecords().ToList();
}
}
//cars现在包含从CSV读取的Car对象列表。
//标题字段(CSV的第一行)已自动与属性名称匹配。
//设置字典。请注意,密钥必须是唯一的。
var carDict=cars.ToDictionary(c=>c.Make);
}
split[0]
是一个字符串,而不是int
。你需要将它解析为一个intMy抱歉,我刚刚更新了我的问题。你不需要道歉…只要想想其他人会如何看待你的问题,因为我们不在你的头脑中。我正在尝试选择每一行并将其拆分为@每个逗号。ToDictionary(拆分=>拆分[0],拆分=>新车(…)代码>split[0]
是一个字符串,而不是int
。尤恩