Hi All,
i want to get total empolyees wise from txt file throught c# please see below expample.
Txt File Value
Emp: 2356 - Total value: 125
Emp: 2389 - Total Value: 130
Emp: 2356 - Total Value: 190
Emp: 2389 - Total Value: 180
Result Show me in RechText box like below
Emp: 2356 - Total Value is: 315
Emp: 2389 - Total Value is: 310
Vinay SinghPosted Jun 16, 2016, 1:48 PM
public class EmpTotalSalary
{
public int EmpId { get; set; }
public int Salary { get; set; }
}
string filepath = @"TextFile1.txt";
var fileStream = new FileStream(filepath, FileMode.Open, FileAccess.Read);
using (var streamReader = new StreamReader(fileStream, Encoding.UTF8))
{
string line;
List<EmpTotalSalary> totalEmpTotalSalary = new List<EmpTotalSalary>();
while ((line = streamReader.ReadLine()) != null)
{
string[] splitLine = line.Split('|');
string[] splitByMid = line.Split('-');
string[] split1 = splitByMid[0].Split(':');
string[] split2 = splitByMid[1].Split(':');
totalEmpTotalSalary.Add(new EmpTotalSalary { EmpId = Convert.ToInt32(split1[1]), Salary = Convert.ToInt32(split2[1]) });
}
var finalData = totalEmpTotalSalary.GroupBy(x => x.EmpId).Select(
g => new
{
EmpId = g.First().EmpId,
Salary = g.Sum(s => s.Salary)
});
}
Karthik ElumalaiPosted Jun 16, 2016, 1:03 PM