1

I have list of list and want to remove duplicate from list. Data is stored in list format say IEnumerable<IEnumerable<string>> tableData if we consider it as table value, parent list is for rows and child list is values of every column. Now I want to delete all duplicate rows. from below table value A is duplicate.

List<List<string>> ls = new List<List<string>>();
ls.Add(new List<string>() { "1", "A" });
ls.Add(new List<string>() { "2", "B" });
ls.Add(new List<string>() { "3", "C" });
ls.Add(new List<string>() { "4", "A" });
ls.Add(new List<string>() { "5", "A" });
ls.Add(new List<string>() { "6", "D" });
IEnumerable<IEnumerable<string>> tableData = ls;

var abc = tableData.SelectMany(p => p).Distinct();   ///not work

after operation, I want abc should be exactly tableData format

ls.Add(new List<string>() { "1", "A" });
ls.Add(new List<string>() { "2", "B" });
ls.Add(new List<string>() { "3", "C" });   
ls.Add(new List<string>() { "6", "D" });
7
  • 2
    What is PropertyData? It would really help if you'd show a short but complete program demonstrating what your data looks like. Commented Jun 3, 2014 at 10:11
  • IEnumerable<IEnumerable<PropertyData>> tableData contains complete table.where each parent list as row and each child list as columns for that row. Commented Jun 3, 2014 at 10:15
  • @jon, I have changed class. Commented Jun 3, 2014 at 10:16
  • @Ehsan, yes its list of list. Commented Jun 3, 2014 at 10:19
  • 2
    It's still unclear what you've got, to be honest. A short but complete example would be much clearer. Commented Jun 3, 2014 at 10:21

4 Answers 4

3
tableData.GroupBy(q => q.Skip(1).First()).Select(q => q.First())
Sign up to request clarification or add additional context in comments.

Comments

2

You can use the overload of Distinct passing in an IEqualityComparer assuming you actually have an IEnumerable<IEnumerable<PropertyData>>.

For example:

var items = tableData.SelectMany(x => x).Distinct(new TableComparer());

And the comparer:

public class TableComparer : IEqualityComparer<PropertyData>
{
    public bool Equals(PropertyData x, PropertyData y)
    {
        return x.id == y.id;
    }

    public int GetHashCode(PropertyData pData)
    {
        return pData.id.GetHashCode();
    }
}

If it's just an IEnumerable<IEnumerable<string>>, you can use Distinct() without the overload:

var items = tableData.SelectMany(x => x).Distinct();

Though your question lacks clarity..

Comments

0
var distinctValues = tableData.SelectMany(x => x).Distinct();

This will flatten your list of lists and select the distinct set of strings.

Comments

0

you can use below menioned code

         List<List<string>> ls=new List<List<string>>();
         ls.Add(new List<string>(){"Hello"});
         ls.Add(new List<string>(){"Hello"});
         ls.Add(new List<string>() { "He" });
         IEnumerable<IEnumerable<string>> tableData = ls;
         var abc = tableData.SelectMany(p => p).Distinct();

O/P is

Hello

He

3 Comments

var abc = tableData.Select(q => q.ElementAt(1).Distinct());
No, that's not it either.
No, It doesn't work. It will not give distinct record.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.