DEV Community

Cover image for How to parse a CSV file from Linq (C#)
Hemant Govekar
Hemant Govekar

Posted on

How to parse a CSV file from Linq (C#)

The entire video is available on my youtube channel :
linq csv

My Youtube Channel (click below image to subscribe)
linq csv

Part of CSV File below.

(Entire file can be downloaded from my github location :

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 */

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);


        private static List<Player> ProcessCSV(string path)
            return File.ReadAllLines(path)
                .Where(row => row.Length > 0)

    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]
Enter fullscreen mode Exit fullscreen mode

Top comments (4)

jeikabu profile image

Just FYI, you can add syntax highlighting:

hemantgovekar profile image
Hemant Govekar • Edited

din't knew has syntal highligting. Thanks for asking for it :-).
Added code highligting

kaylumah profile image
Max Hamulyák

Nice and simple, I like it :-)

hemantgovekar profile image
Hemant Govekar

Thanks Max :-)
More such videos on my youtube channel , subscribe if you like :