LINQ Join a query in a single row

3

I have a LINQ query to which I concatenate the values of a table in a single field to show in DataGridView

Dim Consulta = (From ConsultaConceptos In ds.Tables("Concepto")
                     Select New With { _
                               .Concepto = "Cantidad: " & ConsultaConceptos.Field(Of String)("cantidad") & " ValorUnitario: " & ConsultaConceptos.Field(Of String)("valorUnitario") & " importe:  " & ConsultaConceptos.Field(Of String)("importe") & " Descripción: " & ConsultaConceptos.Field(Of String)("descripcion")
                           })
DataGridView1.DataSource = Consulta.ToList

The fact is that in the table Concepto there can be more than one row, unlike those in more tables that always have only 1.

Example:

____________Concepto_______________
dato             dato             dato      dato  fila1
__________________________________
dato             dato             dato      dato  fila2
__________________________________
dato             dato             dato      dato  fila3

What I want to do is the following:

Show rows 1, 2 and 3 in one

____________Concepto_______________
dato dato dato dato dato dato dato dato dato dato dato dato  fila 1

I know in advance that this is possible because it is already done in an existing system.

    
asked by Diego Cantú 28.03.2016 в 23:45
source

1 answer

1

Taken from the response indicated by @ DiegoCantú from the MSDN site. This publication is intended to contain the response of the MSDN site also in this place. If Jose R. MCP has a user in SOes, please tell him to publish his response to eliminate this publication. It is strongly urged to not vote for or against this response. Visit the original answer at link

  

Ok. Are there operators on VB.net? You could add an implicit conversion operator to String to the Data class. I think that would eliminate the use of () = >, which is called lambda expression.

     

But better not complicate it. Let's make the long route.

public class Dato
{
    //Declaramos una propiedad tipo string para contener el valor.
    public string Valor { get; set; }
    public override string ToString() { return Valor; }
}


//Luego hacemos lo que hacíamos pero coleccionamos objetos tipo Dato, no tipo String como antes.
var filas = from cc in ds.Tables["Concepto"].Rows
            select new Dato() { Valor = String.Format("Cantidad:  {0} ValorUnitario:  {1} Importe:  {2} Descripción:  {3}",
            cc["cantidad"], cc["valorUnitario"], cc["importe"], cc["descripcion"]) };

var listaFilas = filas.ToList();
if (listaFilas.Count > 0)
{
    string r = String.Empty;
    foreach Dato d in listaFilas
  {
      r += " " + d.Valor;
  }
  r = r.Trim();
  listaFilas.Clear();
    Dato d = new Dato()
    {
        Valor = r
    };
    listaFilas.Add(d);
}
DataGridView1.DataSource = listaFilas;
     

Better?

    
answered by 02.04.2016 в 23:14