Default GroupBy() and Distinct() won't do what you want because you are looking for distinct lists, rather than individual values in those lists. Since a List is a reference type, two lists might have the same elements, but Distinct() will still return two lists.
If you want (as you say in your comments):
fnc = ((C1,C2,C3),(C2),(C1,C3),(C1,C3),(C1,C2,C3),(C3)) I want to remove duplicates result will be fnc = ((C1,C2,C3),(C2),(C1,C3),(C3))
Then you can use Distinct, but provide your own Equality Comparer. This requires you to override Equals (to say when the values in both lists are the same) and to implement the GetHashCode() method. Code for GetHashCode() taken from here. If you want to know more about why you need Equals and GetHashCode then see here.
The output of the following code is this:
Fill
( 1 2 )
( 1 )
( 1 2 )
( 1 )
( 1 2 )
De-dupe
( 1 2 )
( 1 )
Code:
static class Program
{
class ListEqualityComparer : IEqualityComparer<List<int>>
{
public bool Equals( List<int> x, List<int> y )
{
return ( x.Count == y.Count ) && new HashSet<int>( x ).SetEquals( y );
}
public int GetHashCode( List<int> list )
{
int hash = 19;
foreach( int val in list )
{
hash = hash * 31 + val.GetHashCode();
}
return hash;
}
}
//Fill is just a method to fill the original lists for testing
static void Fill( List<List<int>> toFill )
{
Console.WriteLine( "Fill" );
for( int i = 1; i <= 5; i++ )
{
Console.Write( "( " );
List<int> newList = new List<int>();
toFill.Add(newList);
for( int j = 1; j <= i%2 + 1; j++ )
{
newList.Add( j );
Console.Write( j + " " );
}
Console.WriteLine( ")" );
}
}
static void Main( string[] args )
{
List<string> result = new List<string>();
List<List<int>> fnc = new List<List<int>>();
Fill( fnc );
var results = fnc.Distinct( new ListEqualityComparer() );
Console.WriteLine( "De-dupe" );
foreach( var list in results )
{
Console.Write( "( " );
foreach( var element in list )
{
Console.Write( element + " " );
}
Console.WriteLine( ")" );
}
}
}
>()`. Are `myList[0]` and `myList[1]` duplicates if they have the same data or only if they share the same reference? You need to be more clear about what `fnc` is and what your requirements for identifying a duplicate are before this question can be answered
– akousmata Jan 20 '14 at 20:57