I have the following method that creates a csv.
var csv = new CsvResult<MatrixCampaignLine>(matrixCampaignLines, fileName, CsvHelper.GetMatrixHeader(), x => x.RegionName,
x => x.CustomerSku.ToString(),
x => x.ProductName.ToString(),
x => x.Quantity.ToString()
);
Happy days, until i need to nest a foreach inside of the last property call. ie i need to iterate thru matrixCampaignLines.Products. So imagines after x.Quantity i added something like this
x => x.开发者_StackOverflow社区Quantity.ToString(),
foreach(var products in x => x.ProductQuantites)
{
x.Quantity,
}
here is the method that is call.
public CsvResult(IEnumerable<T> data, string fileName, List<string> headers, params Func<T, string>[] columns)
{
Data = data;
Columns = columns;
FileName = fileName;
Headers = headers;
}
Edit:
public IEnumerable<T> Data { get; private set; }
public Func<T, string>[] Columns { get; private set; }
public Func<T, string[]>[] Cols { get; private set; }
public string FileName { get; private set; }
public List<string> Headers { get; private set; }
public CsvResult(IEnumerable<T> data, string fileName, List<string> headers, string userName, params Func<T, string[]>[] columns)
{
Data = data;
Cols = columns;
FileName = fileName;
Headers = headers;
}
One way could be to change params Func<T, string>[] columns
to params Func<T, string[]>[] columns
, return the ProductQuantites
as an array and then flatten the nested columns array in CsvResult
.
Edit 2010-10-18
Not pretty and I wish I could come up with something nicer but this seems to work
var products = new List<Product> { new Product {Name="Heliamphora chimantensis", Quantity=7}, new Product {Name="Dionaea muscipula", Quantity=2}};
var mcl = new MatrixCampaignLine { RegionName = "Catalonia", CustomerSku = "4711", ProductName = "Nepenthes rajah", Quantity = 1, ProductQuantites=products };
var data = new List<MatrixCampaignLine> { mcl };
var list = new List<string> { "huvud", "axlar", "knä", "och", "tå" };
var csv = new CsvResult<MatrixCampaignLine>(data, "Jamaica Jan Sun Princess", list, "joshua",
x => new string[] {x.RegionName},
x => new string[] {x.CustomerSku},
x => new string[] {x.ProductName},
x => new string[] {x.Quantity.ToString()},
x => x.ProductQuantites.Select(p => p.Quantity.ToString()).ToArray()
);
精彩评论