【发布时间】:2015-10-28 18:04:16
【问题描述】:
我的目标是将值写入存储在集合中的预先存在的 .csv 文件。目前,我正在读取存储在 .csv 文件中的值,并使用 filerhelper 库将它们存储在一个集合中。但是我想添加 4 列字段{Product ,Base Price,Ship Price,Total Price},然后,如果我的records 集合中的客户名称与customerandproducts 集合中的客户名称匹配 - 写入来自@ 的相应数据987654323@ 集合到我添加的 4 个字段。
这是我的源代码的简化版本:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FileHelpers;
namespace ReadFile
{
class Program
{
static void Main()
{
var engine = new FileHelperEngine<Orders>();
var records = engine.ReadFile("Daniel.csv");
// var customersandproducts = addresses.Select(x => new { x.Name, x.AddressLine1, x.AddressLine2, x.City, x.State, x.S_OrderId, x.PostalCode })
// .Join(products, custs => custs.S_OrderId, prod => prod.P_OrderId,(custs, prod) => new { custs.Name, custs.AddressLine1, custs.AddressLine2, custs.City, custs.State, custs.PostalCode, prod.Title, prod.Quantity, prod.ItemPrice, prod.ShippingPrice });
foreach (var record in records)
{
Console.WriteLine(record.Name);
Console.WriteLine(record.Track);
Console.WriteLine(record.Price);
}
}
}
[DelimitedRecord(",")]
public class Orders
{
public string Name;
public string Track;
public string Price;
}
}
这是我的 .csv 文件中的数据:
ShipToCompanyorName,PackageTrackingNumber,ShipmentInformationTotalShipmentReferenceCharge
customer1,1Z620Y1V0347293915,12.6
customer2,1Z620Y1V0347106126,9.18
customer3,1Z620Y1V0347584931,13.63
customer4,1Z620Y1V0346366348,11.37
customer5,1Z620Y1V0348472309,31.
customer6,1Z620Y1V0348325290,31.34
我的问题:我想将 {Product ,Base Price,Ship Price,Total Price} 添加到 .csv 文件的第一行,然后使用客户名称作为 customerandproducts 集合中的信息填写每一列键。
【问题讨论】:
-
那么你的问题是什么?
-
记录对象长什么样子?
-
那么,.csv 中的每一行都是
Collection<>中的一项?如何分隔 .csv 中的元素?你在用string[] args吗?如果是这样,那似乎有点初级。尝试为您希望数据的外观创建一个类,然后对 .csv 进行序列化/反序列化。 -
我没有使用
string[] args,是的,每一行都是Collection<>中的一项。但我只想提取customerandproducts集合中的特定信息并将此信息添加到正确字段下的正确行 - 即 {Product ,Base Price,Ship Price,Total Price}。 -
您的 .csv 文件的图片?为什么不直接复制/粘贴数据本身以便其他人可以使用?
标签: c# arrays csv filter filehelpers