Concatenate value in repeated records using linq

3

I have a query in linq that returns a list of colonies from a table in SQLSERVER.

var todasColonias = (
                from SQLColonias in context.colonias
                where SQLColonias.status.Equals(true)
                select new
                {
                    _idColonia = SQLColonias.id_colonia,
                    _tipoAsentamiento = SQLColonias.tipo_asentamiento,
                    _descrColonia = SQLColonias.descripcion
                }
            ).ToList();

I need to concatenate the value of the field _typeAssembly when the value of the field _descrColonia exists duplicated in any other record.

In summary, I need to do something like this:

var todasColonias = (
                    from SQLColonias in context.colonias
                    where SQLColonias.status.Equals(true)
                    select new
                    {
                        _idColonia = SQLColonias.id_colonia,
                        _tipoAsentamiento = SQLColonias.tipo_asentamiento,
                        _descrColonia = (SQLColonias.descripcion Existe en cualquier otro registro) ? (_tipoAsentaiento + SQLColonias.descripcion) : (SQLColonias.descripcion)
                    }
                ).ToList();

The intention is to obtain something like this in the field _descrColonia:

_descrColonia = "HAMBURGO"
_descrColonia = "SANTA ROSA"
_descrColonia = "SAN ANTONIO"
_descrColonia = "(FRACCIONAMIENTO) LA ROSITA"
_descrColonia = "(AMPLIACION) LA ROSITA"
_descrColonia = "CENTRO"
_descrColonia = "LOS ANGELES"

In this example the value "LA ROSITA" exists in multiple registers, so the string corresponding to the field _TypeAsentamiento is concatenated.

I know I can go through the list later and apply the concatenation based on techniques such as group () or contains (), but when I tried it I noticed the slowness, so I would prefer to do it directly from the select, is it possible ?

    
asked by Jesús Miraanda 01.10.2018 в 20:26
source

1 answer

1

I understand that you are using the Entity Framework. The solution I give you below is only tested with a List , but I understand that it should work in your case too.

var coloniasList = new List<Colonias>();
coloniasList.Add(new Colonias() { id_colonia = 0, tipo_asentamiento = "(FRACCIONAMIENTO)", descripcion = "LA ROSITA" });
coloniasList.Add(new Colonias() { id_colonia = 1, tipo_asentamiento = "(AMPLIACION)", descripcion = "LA ROSITA" });
coloniasList.Add(new Colonias() { id_colonia = 2, tipo_asentamiento = "(AMPLIACION)", descripcion = "LOS ANGELES" });

var todasColonias = (
        from SQLColonias in coloniasList
        select new
        {
            _idColonia = SQLColonias.id_colonia,
            _tipoAsentamiento = SQLColonias.tipo_asentamiento,
            _descrColonia = coloniasList.Count(x=>x.descripcion==SQLColonias.descripcion)>1? (SQLColonias.tipo_asentamiento + SQLColonias.descripcion) : (SQLColonias.descripcion)
        }
    ).ToList();

As you can see, what I do in the case of _descrColonia is to count the times that the description I'm trying appears on my list, and if it's more than 1 I add the type of settlement.

In your case, the query should be something similar to the following:

var todasColonias = (
                from SQLColonias in context.colonias
                where SQLColonias.status.Equals(true)
                select new
                {
                    _idColonia = SQLColonias.id_colonia,
                    _tipoAsentamiento = SQLColonias.tipo_asentamiento,
                    _descrColonia = context.colonias.Count(x=>x.descripcion==SQLColonias.descripcion)>1? (SQLColonias.tipo_asentamiento + SQLColonias.descripcion) : (SQLColonias.descripcion)
                }
            ).ToList();
    
answered by 02.10.2018 / 13:49
source