-1

i want to convert string to long, can anyone help me .
thank you in advance .

List<ViewAttendenceReport> viewAttendenceReports = new List<ViewAttendenceReport>();
EmployeeAttendenceBLL aEmployeeAttendenceBll = new EmployeeAttendenceBLL();
viewAttendenceReports = aEmployeeAttendenceBll.ViewAttendenceReportDetaitlsInOut(stDate, endDate);


viewAttendenceReports.Add(new ViewAttendenceReport()
{
      EmployeeName = "Total: ",
      TotalWorkingHours = viewAttendenceReports.Count > 0 ? viewAttendenceReports.Sum(a => a.TotalWorkingHours.ToString()) : 0    
});

allAttendenceDataGridView.DataSource = viewAttendenceReports;
sayah imad
  • 1,507
  • 3
  • 16
  • 24
TaTa
  • 9
  • 1
  • 4

2 Answers2

2

You question is not clear, but you said that you want to "convert string to long"

In order to Convert string to long inside a linqtoentities query you could use:

Convert.ToInt64(YourFieldName)

My best guess from your code is that the datatype of the field TotalWorkingHours is string and you want to convert that to long so that you could be able to perform a Sum on that field. And in the end you want to convert the result of sum to string to set it to the TotalWorkingHours field. If that is the case then you could use the following code to achieve that:

viewAttendenceReports.Add(new ViewAttendenceReport()
{
      EmployeeName = "Total: ",
      TotalWorkingHours = viewAttendenceReports.Count > 0 ? viewAttendenceReports.Sum(a => Convert.ToInt64(a.TotalWorkingHours)).ToString() : 0    
});
gwt
  • 2,331
  • 4
  • 37
  • 59
0

I think what you want is to sum the numbers with string type like "1","10","20"... you could use Int32.Parse()/Int64.Parse() instead:

viewAttendenceReports.Add(new ViewAttendenceReport()
{
    EmployeeName = "Total: ",
    TotalWorkingHours = viewAttendenceReports.Count > 0 ? viewAttendenceReports.Sum(a => Int64.Parse(a.TotalWorkingHours)) : 0    
});

Refer to

Convert string to int for sum in linq

Accepted style for long vs Int64 in C#?

Ryan
  • 19,118
  • 10
  • 37
  • 53