我使用功能强大的FileHelpers Library。但是有没有一种内置的方法来搜索生成的对象呢?
var engine = new FileHelperEngine<Text>();
var res = engine.ReadFile("myfile.csv");
string result = res["key"].value;我的csv是这样的: key;value
我的意思是,有没有可能不使用数组,1,12...
也许就像在代码示例中一样。
非常感谢!
发布于 2012-07-11 16:19:27
您可以使用以下命令通过LINQ将生成的数组转换为字典:
var dictionary = validRecords.ToDictionary(r => r.Key, r => r.Value);下面的完整程序演示了这种方法。
[DelimitedRecord(",")]
public class ImportRecord
{
public string Key;
public string Value;
}
class Program
{
static void Main(string[] args)
{
var engine = new FileHelperEngine<ImportRecord>();
string fileAsString = @"Key1,Value1" + Environment.NewLine +
@"Key2,Value2" + Environment.NewLine;
ImportRecord[] validRecords = engine.ReadString(fileAsString);
var dictionary = validRecords.ToDictionary(r => r.Key, r => r.Value);
Assert.AreEqual(dictionary["Key1"], "Value1");
Assert.AreEqual(dictionary["Key2"], "Value2");
Console.ReadKey();
}
}https://stackoverflow.com/questions/11416155
复制相似问题