Click here to Skip to main content
14,828,250 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
[EDIT]duplicated code has been removed and below content has been moved from the title of question[/EDIT]

In csv file I want to read file and then I want to compare their supervior id and id

What I have tried:

C#
using System.Globalization;
using CsvHelper;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//using Microsoft.Azure.Documents;

namespace ConsoleApp5
{
    public class Program
    {

        static void Main(string[] args)
        {
            using var streamReader = File.OpenText(@"C:/Samples/Vivek/ConsoleApp5/Paylocity.csv");
            using var csvReader = new CsvReader(streamReader, CultureInfo.CurrentCulture);
            while (csvReader.Read())
            {
                var CompanyName = csvReader.GetField(0);
                var Id = csvReader.GetField(1);
                var PreferredFirstName = csvReader.GetField(2);
                var FirstName = csvReader.GetField(3);
                var LastName = csvReader.GetField(4);
                var JobTitle = csvReader.GetField(5);
                var WorkPhone = csvReader.GetField(6);
                var WorkExtension = csvReader.GetField(7);
                var Email = csvReader.GetField(8);
                var Supervisor_current = csvReader.GetField(9);
                var Supervisor_ID = csvReader.GetField(10);
                var Hire_Date = csvReader.GetField(11);
                var Rehire_Date = csvReader.GetField(12);
                
               
                Console.WriteLine($"{Id}  {Supervisor_ID}");
            }
        }
    }
}
Posted
Updated 12-Feb-21 1:44am
v2
Comments
Maciej Los 12-Feb-21 3:05am
   
Compare to what?
Member 14709607 12-Feb-21 4:33am
   
compare supervior id and id u see in while loop
Richard MacCutchan 12-Feb-21 3:34am
   
You need to add the code to set the value that you want to compare, and then perform the comparison.

You can use an If statement, see examples here:
https://www.dotnetperls.com/if[^]

When you are working with string values, see:
https://www.dotnetperls.com/string-equals[^]
   
v2
Comments
Maciej Los 12-Feb-21 4:51am
   
;)
RickZeeland 12-Feb-21 5:48am
   
hehe :)
Member 14709607 12-Feb-21 6:32am
   
sir first i want to read csv file and then value are stored then compare their value
RickZeeland 12-Feb-21 7:11am
   
You mean storing the read values in an array?
Member 14709607 12-Feb-21 7:13am
   
Yes i will use another method is split but i split is use , and my excel file have , so the actually right data are not find
Here the code is ,
var exceldata = File.ReadAllLines(@"..\..\Paylocity\Paylocity.csv");
var supervisorID = from paylocity in exceldata
let data = paylocity.Split('?')
select new
{
CompanyName = data[0],
ID =data[1],
PreferredFirstName = data[2],
FirstName = data[3],
LastName = data[4],
JobTitle_Current =data[5],
Work_Phone = data[6],
WorkExtension = data[7],
E_mail = data[8],
SupervisorCurrent = data[9],
SupervisorEmployeeID_Current = data[10],
HireDate_Current = data[11],
RehireDate_Curret =data[12]

};
foreach (var pay in supervisorID)
{
if (pay.CompanyName != null && pay.SupervisorEmployeeID_Current == pay.ID)
{
Console.WriteLine(pay.CompanyName);
}
else
{
Console.WriteLine(pay.LastName);
}

}
Console.WriteLine("Press any key to exit");
Console.ReadLine();
}
RickZeeland 12-Feb-21 7:35am
   
Ah, I see, working on it ...
Member 14709607 13-Feb-21 1:30am
   
i try this but is not work
I think you should be able to use a class like this:
/// <summary>
/// Read from CSV file into an array of class People
/// </summary>
public static void ReadCsv()
{
    IEnumerable<People> records;

    using (var streamReader = File.OpenText(@"C:/Samples/Vivek/ConsoleApp5/Paylocity.csv"))
    using (var csvReader = new CsvReader(streamReader, CultureInfo.CurrentCulture))
    {
        records = csvReader.GetRecords<People>();

        foreach (var record in records)
        {
            if (record.Id == record.Supervisor_ID)
            {
                Console.WriteLine($"{record.Id}  {record.Supervisor_ID}");
            }
        }
    }
}

public class People
{
    public int Id { get; set; }
    public string PreferredFirstName { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    // etc.
    public int Supervisor_ID { get; set; }
    // etc.
}

note that this is not a complete example, it is up to you to complete the class ...

If you get a header error, take a look at the documentation about HeaderValidated here:
CsvHelper[^]

You might need to use:
csvReader.Configuration.HasHeaderRecord = false;
   
v3
Comments
Member 14709607 13-Feb-21 1:31am
   
Unhandled Exception: CsvHelper.HeaderValidationException: Header with name 'CompanyName'[0] was not found.
Header with name 'Id'[0] was not found.
Header with name 'PreferredFirstName'[0] was not found.
Header with name 'FirstName'[0] was not found.
Header with name 'LastName'[0] was not found.
Header with name 'JobTitle_Current'[0] was not found.
Header with name 'WorkPhone'[0] was not found.
Header with name 'WorkExtension'[0] was not found.
Header with name 'Email'[0] was not found.
Header with name 'SupervisorCurrent'[0] was not found.
Header with name 'SupervisorEmployee_ID'[0] was not found.
Header with name 'HireDate'[0] was not found.
If you are expecting some headers to be missing and want to ignore this validation, set the configuration HeaderValidated to null. You can also change the functionality to do something else, like logging the issue.

IReader state:
ColumnCount: 0
CurrentIndex: -1
HeaderRecord:
[""]
IParser state:
ByteCount: 0
CharCount: 208
Row: 1
RawRow: 1
Count: 13
RawRecord:
Company Name,ID,Preferred First Name,First Name,Last Name,Job Title - Current,Work Phone,Work Extension,E-mail,Supervisor - Current,Supervisor Employee ID - Current,Hire Date - Current,Rehire Date - Current

>>they can not found
u have to only explain or give me example then i will do it
RickZeeland 13-Feb-21 1:40am
   
You probably have no header in your CSV file, as it says in the error message:
set the configuration HeaderValidated to null
Member 14709607 13-Feb-21 1:49am
   
but we use IEnumerable and its say does not contain defination for Configuration
RickZeeland 13-Feb-21 1:56am
   
You can also add a header to your CSV file if that is easier

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900