-1

This is my SQL table

CREATE TABLE PlayerXP 
(
    id_partida INT NOT NULL IDENTITY(1, 1) PRIMARY KEY,
    ganhoxp    NVARCHAR(5) NOT NULL,
    duracao    VARCHAR(5) NOT NULL,
)

This code to the linq format

SELECT
    SUM (60 * CONVERT(int, LEFT( duracao, 2)) +
         CONVERT(int, RIGHT(duracao, 2))) AS TotalSegundos
FROM PlayerXP;

The column duracao is formatted as Minute/Seconds (mm:ss)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Perhaps my [SQL to LINQ Recipe](https://stackoverflow.com/questions/49245160/sql-to-linq-with-multiple-join-count-and-left-join/49245786#49245786) might help you. – NetMage Aug 24 '20 at 17:08
  • I am a beginner in programming and I am still learning Linq, so it is difficult to have the basis to write my own code, learning alone, SQL already has some domain. But I will definitely look more at the documentation – Leandro Haiuken Aug 24 '20 at 17:57
  • With a little more research I discovered another method (https://stackoverflow.com/questions/25013073/how-to-sum-time-from-a-datagridview-columns) that also worked, from my DataGridView, which was taking this data from the db and showing it on the Form – Leandro Haiuken Aug 24 '20 at 17:59

1 Answers1

0

Suppose the table were a business entity:

public class PlayerXP 
{
 public int id_partida;
 public string ganhoxp;
 public string duracao;
}

with an object

PlayerXP playerxp = new PlayerXP()

and if the value of duracao in an object of that business entity class were

playerxp.duracao = "12:60"

then, Try this:

int TotalSegundos = (playerxp.duracao.Split(":")
                              .Select(x => Int32.Parse(x, CultureInfo.InvariantCulture))
                              .First() * 60) + playerxp.duracao.Split(":")
                                                      .Select(x => Int32.Parse(x, CultureInfo.InvariantCulture))
                                                      .Last();

the TotalSegundos value i got is 780

Aditya Nair
  • 514
  • 1
  • 9
  • 18