You must Sign In to post a response.
  • Category: LINQ

    LINQ to CSV (comma seprated value) File

    Hello All
    Here is code for LINQ to CSV file. Read csv file and then calculate Minimum,Maximum,Total,Average,Result for each student


    IEnumerable<string> lstcsv = File.ReadLines("D:\\1.csv");
    var results = from data in lstcsv
    let temp = data.Split(',')
    .Skip(1)
    .Select(x => Convert.ToInt32(x))
    select new
    {
    Maximum = temp.Max(),
    Minimum = temp.Min(),
    Total = temp.Sum(),
    Avgerage = temp.Average()
    };

    var query = results.ToList();

    foreach (var x in query)
    {
    Console.WriteLine(
    string.Format("Maximum: {0}, " +
    "Minimum: {1}, " +
    "Total: {2}, " +
    "Average: {3}, Result :{4}"
    ,
    x.Maximum, x.Minimum, x.Total, x.Avgerage, (x.Avgerage > 70) ? "Distinction" : (x.Avgerage > 60 && x.Avgerage < 70) ? "First Class" : (x.Avgerage > 50 && x.Avgerage < 60) ? "Second Class" : (x.Avgerage > 400 && x.Avgerage < 50) ? "Pass" : "Fail"));
    }


    Thanks
    Umesh Bhosale
  • #755144
    Nice POC ... Thanks

    Would be great if you also provide the input file (1.csv) for this POC.

    Regards.

  • #755165
    Yes Sharad
    Here 1.csv

    Thanks

    1.csv

    Delete Attachment


  • Sign In to post your comments