views:

440

answers:

2

I've a problem!

I've a

List <List<Class>> L = new List<List<Class>>();

where Class contains

IDLinea
Posizione

And so on...

I want to have a Distinct List<List<Class>> Where there are no objects with same lines.

For example:

List <List<Class>> L = new List<List<Class>>();

var A = new List<Class>();

Class C = new Class();
C.IDLinea = 1;
C.Posizione = 5;

A.Add(C);

C = new Class();
C.IDLinea = 2;
C.Posizione = 1;

A.Add(C);

L.Add(A);

var B = new List <Class>();

C = new Class();
C.IDLinea = 1;
C.Posizione = 3;

B.Add(C);

C = new Class();
C.IDLinea = 2;
C.Posizione = 1;

B.Add(C);

L.Add(B);

var D = new List <Class>();

C = new Class();
C.IDLinea = 4;
C.Posizione = 1;

D.Add(C);

C = new Class();
C.IDLinea = 2;
C.Posizione = 4;

D.Add(C);

L.Add(D);

So I want A and D

I hope that what I need is clear.

Thanks.

A: 
  1. Use a List
  2. Have your class ovveride Equals.
  3. Use the Contains method of the list.
Bogdan Gavril
Could you write me an example? Thanks!
Giomuti
+3  A: 

Edited based on your further explanation

enum LineComparisonMode
{
   ///<summary>the order of the line numbers matters, default</summary>
   Ordered = 0, 
   ///<summary>the order of the line numbers doesn't matter
   /// Has a higher cost to the default.
   ///</summary>
   Unordered = 1,
} 

class ListLineComparer : IEqualityComparer<List<Class>>
{
    class LineComparer : IEqualityComparer<Class>
    {
        public bool Equals(Class x, Class y)
        {
            return x.IDLinea == y.IDLinea;
        }

        public int GetHashCode(Class x)
        {
            return x.IDLinea;
        }
    }

    private readonly LineComparer lines;
    private readonly LineComparisonMode mode;

    public ListLineComparer() {}

    public ListLineComparer(LineComparisonMode mode)
    { 
        this.mode = mode;
    }

    public bool Equals(List<Class> x, List<Class> y)
    {
        if (mode == LineComparisonMode.Ordered)
            return x.SequenceEqual(y, lines);
        else
            return x.OrderBy<Class, int>(Line).SequenceEqual(y.OrderBy<Class, int>(Line), lines);
    }

    private static int Line(Class c)
    {
        return c.IDLinea;
    }

    public int GetHashCode(List<Class> x)
    {
        //this is not a good hash (though correct) 
        // but not relevant to current question
        return x.Sum(c => c.IDLinea);
    }
}

// assume List <List<Class>> L = new List<List<Class>>(); from question
var result = L.Distinct(new ListLineComparer());

Result in form of IList of IList of Class rather than Lists hopefully that is acceptable. If not you will have the build the lists yourself rather than letting Linq deal with it. If you want to have lines {1,2,3} be considered equal to {3,2,1} then you would need to set the Mode.

var result = L.Distinct(new ListLineComparer(LineComparisonMode.Unordered));

This is all a little more verbose that I would have done it myself but makes it clear what is going on/what assumptions are being made so it is simple to change (if say the column position becomes involved later).

ShuggyCoUk
There's an error with you code. The number of list < Class > is not fixed and I need to verify the correct sequence so 1-2-3 is different from 3-2-1 and if the list is1-2-3<br>1-2-3<br>3-1<br>I need as result <br>1-2-3<br>3-1<br>I hope you help me.Thanks
Giomuti
Ah your original question didn't make this clear, you simply wish to remove duplicates, is 1-2-3 considered the same as 3-2-1?
ShuggyCoUk
No, It's Different! Thanks for all and for your patiente! Bye.
Giomuti
@Giomuti - so is it correct now? if so an accept would be nice.
ShuggyCoUk
It returns this error:Error 3 'CotrapClassi.ListLineComparer.LineComparer' does not implement interface member 'System.Collections.Generic.IComparer<CotrapClassi.Linea>.Compare(CotrapClassi.Linea, CotrapClassi.Linea)' ListLineComparer.cs 11 15 CotrapClassi
Giomuti
Are you sure these classes function?
Giomuti
Sorry - typing from memory and typed a few methods wrong. new version compiles and works
ShuggyCoUk