Home > Back-end >  Uppercase a List of object c# [duplicate]
Uppercase a List of object c# [duplicate]

Time:09-29

I have this class:

  public class Customer
    {
        public string Name { get; set; }
        public string Phone { get; set; }
        public string Email { get; set; }
        public string Contact { get; set; }


    }

and i have a code to get data from excel with EPPLUS and put it in the list. I wanted to add a new column called name_upper where all the names will appear in uppercase(). Code:

static void Main(string[] args)
        {

            var customer = ReadXls();

            foreach (var item in customer)
            {
                Console.WriteLine($"Name:{item.Name}\n Phone:{item.Phone}\nContact:{item.Contact}\nEmail:{item.Email}\n");
            }
        }
        private static List<Customer> ReadXls()
        {
            var response = new List<Customer>();

            string FileName;
            Console.WriteLine("Diretório do ficheiro:");
            FileName = Console.ReadLine();

            FileInfo existingFile = new FileInfo(FileName);

            ExcelPackage.LicenseContext = LicenseContext.NonCommercial;

            using(ExcelPackage package = new ExcelPackage(existingFile))
            {
                ExcelWorksheet worksheet = package.Workbook.Worksheets[0];
                int colCount = worksheet.Dimension.End.Column;

                int rowCount = worksheet.Dimension.End.Row;

                int row = 2;
                int col = 1;

               while(string.IsNullOrWhiteSpace(worksheet.Cells[row,col].Value?.ToString()) == false)
                {
                    Customer customer = new();

                        customer.Name = worksheet.Cells[row, 4].Text.Trim();
                        customer.Phone = worksheet.Cells[row, 5].Text.Trim();
                        customer.Contact = worksheet.Cells[row, 7].Text.Trim();
                        customer.Email = worksheet.Cells[row, 8].Text.Trim();
                        response.Add(customer);
                    
                    row  = 1;
                }
            }
            return response;
        }


Now I need the Names to appear in uppercase I don't know where and how to do it. How to make only the name appear in uppercase.

CodePudding user response:

Just use the ToUpper() method which converts every character to it's uppercase version

customer.Name = worksheet.Cells[row, 4].Text.Trim().ToUpper();

More about ToUpper - https://docs.microsoft.com/en-us/dotnet/api/system.string.toupper?view=net-5.0

  • Related