Grouping with List.
I am processing records from a CSV file. The records look like this:
The output needs to look like this! Ignore the first row in the CSV for now.
Here is the code I have in LINQPAD. How can I reduce the number of lines of code?
Location,ProductType,ProductCode,PhotoName
Montreal,Complex,1030867,//netApp01/Galaxy/Assets/Migrated/2013/08/116576_1030867a.jpg
Montreal,Complex,1030867,//netApp01/Galaxy/Assets/Migrated/2013/08/116576_1030867b.jpg
Montreal,Complex,1030867,//netApp01/Galaxy/Assets/Migrated/2013/08/116576_1030867c.jpg
The output needs to look like this! Ignore the first row in the CSV for now.
{
"Location": "Montreal",
"ProductCode": "1030867",
"Title": "Complex",
"Photopaths":
["//netApp01/Galaxy/AFDev1/Assets/Migrated/2013/08/116576_1030867a.jpg",
"//netApp01/Galaxy/AFDev1/Assets/Migrated/2013/08/116576_1030867b.jpg",
"//netApp01/Galaxy/AFDev1/Assets/Migrated/2013/08/116576_1030867c.jpg"]
}
Here is the code I have in LINQPAD. How can I reduce the number of lines of code?
const string fileName = @"C:\temp\AssetSpec.csv";
String[] lines = File.ReadAllLines(fileName);
//output the lines
//lines.Dump();
List<AssetSpec> listofSpecs = new List<AssetSpec>();
//Declare a list of the class
List<AssetSpec> listofSpecs = new List<AssetSpec>();
foreach (var l in lines)
{
var x = l.Split(',');
listofSpecs.Add(new AssetSpec()
{
Location = x[0],
ProductCode = x[2],
ProductType = x[1],
PhotoPath = x[13], //Non repeating column
});
}
ExpectedFormat ef = new ExpectedFormat();
var g = listofSpecs.Select(s => new { a = s.Location, b = s.ProductCode, c = s.ProductType}).Distinct();
listofSpecs.Select(s => new { e = s.PhotoPath }).ToList().Dump();
//How can I reduce this block of code
//*************************************************
ef.Location = g.Select(s => s.a).FirstOrDefault();
ef.ProductCode = g.Select(s => s.b).FirstOrDefault();
ef.ProductType = g.Select(s => s.d).FirstOrDefault();
//*************************************************
ef.Photopaths = listofSpecs.Select(s => s.PhotoPath ).ToList();
ef.Dump();
string aas = JsonConvert.SerializeObject(ef); //references newtonsoft JSON.NET
aas.Dump();
}
public class ExpectedFormat
{
public string Location { get; set; }
public string ProductCode { get; set; }
public string ProductType { get; set; }
public List<String> Photopaths { get; set;}
}
public class AssetSpec
{
public string Location { get; set; }
public string ProductCode { get; set; }
public string ProductType { get; set; }
public String PhotoPath {get;set;}
}
Comments
Secondly, I suspect the reason it may not compile is that I took away a lot of fields from the original object for keeping it concise. I may have missed something in that process.
Here is the code that will compile.
void Main() { const string fileName = @"C:\temp\AssetSpec.csv"; String[] lines = File.ReadAllLines(fileName); //output the lines //lines.Dump(); List<AssetSpec> listofSpecs = new List<AssetSpec>(); //Declare a list of the class foreach (var l in lines) { var x = l.Split(','); listofSpecs.Add(new AssetSpec() { Location = x[0], ProductCode = x[2], ProductType = x[1], PhotoPath = x[13], //Non repeating column }); } ExpectedFormat ef = new ExpectedFormat(); var g = listofSpecs.Select(s => new { a = s.Location, b = s.ProductCode, c = s.ProductType }).Distinct(); listofSpecs.Select(s => new { e = s.PhotoPath }).ToList().Dump(); //How can I reduce this block of code //************************************************* ef.Location = g.Select(s => s.a).FirstOrDefault(); ef.ProductCode = g.Select(s => s.b).FirstOrDefault(); ef.ProductType = g.Select(s => s.c).FirstOrDefault(); //************************************************* ef.Photopaths = listofSpecs.Select(s => s.PhotoPath).ToList(); ef.Dump(); } public class ExpectedFormat { public string Location { get; set; } public string ProductCode { get; set; } public string ProductType { get; set; } public List<String> Photopaths { get; set; } } public class AssetSpec { public string Location { get; set; } public string ProductCode { get; set; } public string ProductType { get; set; } public String PhotoPath { get; set; } } // Define other methods and classes here