views:

117

answers:

4

I have two lists List that i need to combine and removing duplicate values of both lists

A bit hard to explain, so let me show an example of what the code looks like, and what i want as a result, in sample I use int type not ResultadoDeAnalisisFicheroSql class.

first_list = [1, 12, 12, 5]

second_list = [12, 5, 7, 9, 1]

The result of combining the two lists should result in this list: resulting_list = [1, 12, 5, 7, 9]

You'll notice that the result has the first list, including its two "12" values, and in second_list has an additional 12, 1 and 5 value.

ResultadoDeAnalisisFicheroSql class

[Serializable]
    public partial class ResultadoDeAnalisisFicheroSql
    {
        public string FicheroSql { get; set; }

        public string RutaFicheroSql { get; set; }

        public List<ErrorDeAnalisisSql> Errores { get; set; }

        public List<RecomendacionDeAnalisisSql> Recomendaciones { get; set; }

        public ResultadoDeAnalisisFicheroSql()
        {

        }

        public ResultadoDeAnalisisFicheroSql(string rutaFicheroSql)
        {
            if (string.IsNullOrEmpty(rutaFicheroSql)
                || rutaFicheroSql.Trim().Length == 0)
            {
                throw new ArgumentNullException("rutaFicheroSql", "Ruta de fichero Sql es nula");
            }

            if (!rutaFicheroSql.EndsWith(Utility.ExtensionFicherosErrorYWarning))
            {
                throw new ArgumentOutOfRangeException("rutaFicheroSql", "Ruta de fichero Sql no tiene extensión " + Utility.ExtensionFicherosErrorYWarning);
            }

            RutaFicheroSql = rutaFicheroSql;
            FicheroSql = ObtenerNombreFicheroSql(rutaFicheroSql);
            Errores = new List<ErrorDeAnalisisSql>();
            Recomendaciones = new List<RecomendacionDeAnalisisSql>();
        }

        private string ObtenerNombreFicheroSql(string rutaFicheroSql)
        {
            var f = Path.GetFileName(rutaFicheroSql);
            return f.Substring(0, f.IndexOf(Utility.ExtensionFicherosErrorYWarning));
        }


        public override bool Equals(object obj)
        {
            if (obj == null)
                return false;
            if (!(obj is ResultadoDeAnalisisFicheroSql))
                return false;

            var t = obj as ResultadoDeAnalisisFicheroSql;
            return t.FicheroSql == this.FicheroSql
                && t.RutaFicheroSql == this.RutaFicheroSql
                && t.Errores.Count == this.Errores.Count
                && t.Recomendaciones.Count == this.Recomendaciones.Count;
        }


    }

Any sample code for combine and removing duplicates ?

+11  A: 

Have you had a look at Enumerable.Union

This method excludes duplicates from the return set. This is different behavior to the Concat method, which returns all the elements in the input sequences including duplicates.

List<int> list1 = new List<int> { 1, 12, 12, 5};
List<int> list2 = new List<int> { 12, 5, 7, 9, 1 };
List<int> ulist = list1.Union(list2).ToList();
astander
+1 to counter the -1, seems like a viable answer to me
PostMan
sorry ... couldn't believe my eyes, but i double-checked it, so i've removed downvote and comment and added a line in my answer! .. sorry!
Andreas Niedermair
Its fine, it is actually that simple X-)
astander
+1 | nice one, didn't know that either!
Shaharyar
i have a problem with this... i wrote that but its not working... can u help me?
Dr TJ
What seems to be the problem
astander
when i use `Collection<Person>` its not working and just passing all items...
Dr TJ
@Dr TJ: Do your person Class implements IEqualityComparer<T>? If so, you'll need to check your GetHashCode and Equals methods. See the Remarks section of http://msdn.microsoft.com/en-us/library/bb341731.aspx.
Tomas Narros
+4  A: 

why not simply eg

var newList = list1.Union(list2)/*.Distinct()*//*.ToList()*/;

oh ... according to msdn you can leave out the .Distinct()

This method excludes duplicates from the return set

Andreas Niedermair
+3  A: 

Use Linq's Union:

using System.Linq;
var l1 = new List<int>() { 1,2,3,4,5 };
var l2 = new List<int>() { 3,5,6,7,8 };
var l3 = l1.Union(l2).ToList();
Robert Jeppesen
+3  A: 
    List<int> first_list = new List<int>() {
        1,
        12,
        12,
        5
    };

    List<int> second_list = new List<int>() {
        12,
        5,
        7,
        9,
        1
    };

    var result = first_list.Union(second_list);
Shaharyar
You do not have to call Distinct, read the documentation http://msdn.microsoft.com/en-us/library/bb341731.aspx, or test it out yourself...
astander
Yup, I just saw it! - updated my answer.
Shaharyar
If i use class T for List<T>, is the same like List<int> ?
alhambraeidos
Yes, it will do the trick just fine.
Shaharyar