How to parse a CSV file from Linq (C#)
Tags: csharp, linq, csv
Published: 2021-04-12 08:19:03

The entire video is available on my youtube channel :
<a href="https://youtu.be/3tnLIBL0J2k" target="_blank"><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d28n62vd7kkl2r13vsxh.PNG" alt="linq csv" width="240" height="180" border="10" /></a>

My Youtube Channel (click below image to subscribe)
<a href="https://www.youtube.com/channel/UCajNSAb41SHFYAsCJxUKIMw?sub_confirmation=1" target="_blank"><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lwopxxlcfddz4hm4cq4t.PNG" alt="linq csv" width="780" height="180" border="10" /></a>

### Part of CSV File below. 
####(Entire file can be downloaded from my github location : 
#### https://github.com/hemantgovekar/ParseCSVWithLinq/blob/master/ParseCSV/Player.csv)

Player_Id,Player_Name,DOB,BattingHand,BowlingSkill,Country,Is_Umpire,
1,SC Ganguly,08-Jul-72,Left_Hand,Right-arm medium,India,0,
2,BB McCullum,27-Sep-81,Right_Hand,Right-arm medium,New Zealand,0,
3,RT Ponting,19-Dec-74,Right_Hand,Right-arm medium,Australia,0,
4,DJ Hussey,15-Jul-77,Right_Hand,Right-arm offbreak,Australia,0,
5,Mohammad Hafeez,17-Oct-80,Right_Hand,Right-arm offbreak,Pakistan,0,
6,R Dravid,11-Jan-73,Right_Hand,Right-arm offbreak,India,0,
7,W Jaffer,16-Feb-78,Right_Hand,Right-arm offbreak,India,0,
8,V Kohli,05-Nov-88,Right_Hand,Right-arm medium,India,0,
9,JH Kallis,16-Oct-75,Right_Hand,Right-arm fast-medium,South Africa,0,
10,CL White,18-Aug-83,Right_Hand,Legbreak googly,Australia,0,
11,MV Boucher,03-Dec-76,Right_Hand,Right-arm medium,South Africa,0,
12,B Akhil,07-Oct-77,Right_Hand,Right-arm medium-fast,India,0,
13,AA Noffke,30-Apr-77,Right_Hand,Right-arm fast-medium,Australia,0,
14,P Kumar,02-Oct-86,Right_Hand,Right-arm medium,India,0,
15,Z Khan,07-Oct-78,Right_Hand,Left-arm fast-medium,India,0,
.......
.......
.......
.......

#### Code base


/* C# File to access csv */
/* using statements */
```C#
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ParseCSV
{
    class Program
    {
        static void Main(string[] args)
        {
            var players = ProcessCSV("Player.csv");

            foreach (var player in players)
            {
                Console.WriteLine(player.PlayerName + ' ' + player.Country);
            }

            Console.ReadLine();
        }

        private static List<Player> ProcessCSV(string path)
        {
            return File.ReadAllLines(path)
                .Skip(1)
                .Where(row => row.Length > 0)
                .Select(Player.ParseRow).ToList();
        }
    }

    public class Player
    {
        public int Id { get; set; }
        public string PlayerName { get; set; }
        public string PayerDOB { get; set; }
        public string BattingHand { get; set; }
        public string BowlingSkills { get; set; }
        public string Country { get; set; }
        public string IsUmpire { get; set; }

        internal static Player ParseRow(string row)
        {
            var columns = row.Split(',');
            return new Player()
            {
                Id = int.Parse(columns[0]),
                PlayerName = columns[1],
                PayerDOB = columns[2],
                BattingHand = columns[3],
                BowlingSkills = columns[4],
                Country = columns[5],
                IsUmpire = columns[6]
            };
        }
    }
}
```