LINQ UNION-ADDRANGE DIFFERENCE – DIFERENCIA


Hoy me preguntaron si no era lo mismo hacer un AddRange en una colección que un Union de LINQ y claramente la respuesta es NO!.

Por qué no es lo mismo hacer un AddRange que hacer un Union y asignar a la misma colección?

Porque el AddRange repite información en cambio el Union no agrega datos repetidos (salvo que sean instancias distintas con colecciones de objetos).


Today a developer asked me if using AddRange over a collection was the same that using LINQ’s UNION, and as a matter of fact the answer was “NO!”.

Why is not the same to do AddRange or to do Union and assign to same collection?

Because the AddRange repeats information, on the other hand, UNION does not add repeated data (unless they are different instances with object collections).

Veamos código (Let’s check out code)…

class Program
{
static void Main(string[] args)
{
Console.WriteLine(“*******************************”);
Console.WriteLine(“Probando Union y AddRange”);
Console.WriteLine(“*******************************”);

Console.WriteLine(“Agrego a la lista nros del 1 al 10”);
List list = GetNewList(0, 10);

Console.WriteLine(“Creo otra lista de nros del 1 al 10”);
List list2 = GetNewList(0, 10);

Console.WriteLine(“Hago un Union y muestro”);
list = list.Union(list2).ToList();

foreach (var l in list)
{
Console.WriteLine(“Nro: {0}”, l);
}

Console.WriteLine(“Reinicio listas, hago AddRange y muestro”);
list = GetNewList(0, 10);
list.AddRange(list2);

foreach (var l in list)
{
Console.WriteLine(“Nro: {0}”, l);
}

Console.ReadKey();
}

static List GetNewList(int baseNumber, int length)
{
List rv = new List();
for (int i = baseNumber; i < length; i++)
{
rv.Add(i);
}
return rv;
}
}

unionaddrange

About the author:

Matías Creimerman

Matías Creimerman
I’m a specialist in design, development and management of software solutions with almost 20 years of experience. Microsoft Certificated Professional (MCP). Expert in dot net and Microsoft technologies. Experience and skills in designing solutions in a wide range of commercial, industrial and production areas. Design of architectures, software applications and processes. Skills in leadership and team management. Tech trainer. Technology researcher. Self-taught and dedicated to continuous learning. Skills in estimation, quotation, projects proposals and solutions design. Entrepreneurial spirit. Strong Tech profile but also customer oriented. I perform roles as fullstack dev, tech consultant, technical referent, development leader, team leader, architect, cross leader, tech manager, tech director, trainer, ramp-up & follow-up teams, software factory manager, DevOps and release manager. Regular chess player and musician.

Professional Website

In

Blogger

Github

About Me

Portfolio

Wordpress - Arquitectura y desarrollo de software

Wordpress - Personal Blog

Microsoft - Youracclaim Badges

Microsoft - Tech Profile

Microsoft - ASP.NET Forum

tw
Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License
Creative Commons License
This content is property of Matias Creimerman Any misuse of this material will be punishable
This work is licensed under a International Copyright Law protects “original works of authorship”
including photographs, videos, and blog posts posted on social media sites
The content has no rights to be shared without authorization or citation to the author.
This content cannot be sold be adapted or modified partially or totally.
All content shared outside this blog that doesn’t belong to the author must have citations to the author.

Publicado por:

Matias Creimerman

I’m a specialist in design, development and management of software solutions with 20 years of experience. Microsoft Certificated Professional (MCP). Expert in dot net and Microsoft technologies. Experience and skills in designing solutions in a wide range of commercial, industrial and production areas. Design of architectures, software applications and processes. Skills in leadership and team management. Tech trainer. Technology researcher. Self-taught and dedicated to continuous learning. Skills in estimation, quotation, projects proposals and solutions design. Entrepreneurial spirit. Strong Tech profile but also customer oriented. I perform roles as fullstack dev, tech consultant, technical referent, development leader, team leader, architect, cross leader, tech manager, tech director, trainer, ramp-up & follow-up teams, software factory manager, DevOps and release manager. Regular chess player and musician.

Categorías CódigoEtiquetas , , , , , , , , Deja un comentario

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión /  Cambiar )

Google photo

Estás comentando usando tu cuenta de Google. Cerrar sesión /  Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión /  Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión /  Cambiar )

Conectando a %s