locked
Adding Spaces to Column Names in LINQ Select Query RRS feed

  • Question

  • User271039688 posted

    Hi,

    I have the following code that attempts to extract data from a table and place it in a CSV file ready to be imported into another system. The problem I have is that I need the column names returned to include a space but I cannot figure out how I can do this. For example, you will notice that my select new query has the field names as Code and Address but I need these to be Code(R) and Address 1. Is this possible please?

            CsvFileDescription csvFileDescription = new CsvFileDescription
                {
                    SeparatorChar = ',',
                    //FirstLineHasColumnNames = true,
                    //EnforceCsvColumnAttribute = true,
                    IgnoreUnknownColumns = true,
                };
                CsvContext csvContext = new CsvContext();
    
    
                var customers = from c in context.customer_data
                                where c.customer_is_active != false
                                orderby c.customer_name
                                select new { Code = c.customer_id, Address = c.customer_address1 };
    
                csvContext.Write(customers, Path.Combine(Server.MapPath("~"), "CSV", "Customers.csv"), csvFileDescription);
    

    Thursday, November 1, 2018 11:12 AM

All replies

  • User-474980206 posted
    According to the docs ( always good to read), you control column names with a field attribute. Just make a custom class with the column names you want, and use this in the select, instead of an anonymous class.
    Thursday, November 1, 2018 2:12 PM
  • User-271186128 posted

    Hi webdeveloper2016,

    my select new query has the field names as Code and Address but I need these to be Code(R) and Address 1

    According to your code, it seems that you are using LinqToCsv package to export data to csv file. If that is the case, you could try to use the CsvColumn property to set the columns in the csv file.

    Code like this:

        class Product
        {
            [CsvColumn(Name = "Product Name", FieldIndex = 1)]
            public string Name { get; set; }
            [CsvColumn(FieldIndex = 2, OutputFormat = "dd MMM HH:mm:ss")]
            public DateTime LaunchDate { get; set; }
            [CsvColumn(FieldIndex = 3, CanBeNull = false, OutputFormat = "C")]
            public decimal Price { get; set; }
            [CsvColumn(FieldIndex = 4)]
            public string Country { get; set; }
            [CsvColumn(FieldIndex = 5)]
            public string Description { get; set; }
        }

    and C# code as below:

                List<Product> products2 = new List<Product>()
                {
                    new Product(){ Name="product A", Country="US"},
                    new Product(){ Name="product A", Country="US"},
                    new Product(){ Name="product A", Country="US"},
                };
                
                CsvFileDescription outputFileDescription = new CsvFileDescription
                {
                    SeparatorChar = '\t', // tab delimited
                    FirstLineHasColumnNames = true, // no column names in first record
                    FileCultureName = "nl-NL" // use formats used in The Netherlands
                };
                CsvContext cc = new CsvContext();
                cc.Write(
                    products2,
                    "products2.csv",
                    outputFileDescription);

    The output :

    More details, please refer to the LINQ to CSV library.

    Best regards,
    Dillion

    Wednesday, November 14, 2018 8:54 AM