Error selecting part of a string in select of an entity using Entity Framework and Linq

0

I need to mount a drop-down with part of the name of a project, which is very large in the database, going up to 250 characters. For this I made a selector like this:

var lista = _projetoAppServ.ObterTodos("Descricao")
      .Select( a => new { 
                          ProjetoId= a.ProjetoId, 
                          Descricao = a.Descricao.Substring(0,50) 
                        })

However, there are projects with less than 50 characters, causing the error below: Index and length must refer to a location within the string. Parameter name: length

How can I solve this problem?

    
asked by anonymous 30.11.2017 / 13:30

1 answer

1

You can use PadRight to fill in spaces.

var lista = _projetoAppServ.ObterTodos("Descricao")
      .Select( a => new { 
                          ProjetoId= a.ProjetoId, 
                          Descricao = a.Descricao.PadRight(50, ' ').Substring(0,50) 
                        })
    
30.11.2017 / 13:35