Grouping with List.

I am processing records from a CSV file. The records look like this:

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

  • Firstly, your example does not even compile, and as far as I know the standard JsconConvert will not produce output exactly like you describe, but ignoring these issues, it looks like you are grouping your csv by Location, ProductCode and ProductType and returning a List (or array) of ExpectedFormat objects, ie something like
    
    	 List<ExpectedFormat> output = (from r in listofSpecs
    	 group r by new { r.Location, r.ProductCode, r.ProductType } into results
    	 select new ExpectedFormat
    	 {
    		 Location       = results.Key.Location,
    		 ProductCode 	= results.Key.ProductCode,
    		 ProductType 	= results.Key.ProductType,
    		 Photopaths 	= results.Select(a => a.PhotoPath).ToList()
    	 }).ToList();
    	 
  • Firstly, thanks for the help.
    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
  • Still think my code is probably what you want.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!