Click here to Skip to main content
15,920,633 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
List<int,string> list = new List<int,string>();

//Example say, if the values in my list is

list[0] ={100, "abc"}
list[1] ={101, "zzz"}
list[2] ={103, "aaa"}
list[3] ={100, "yyy"}


//I want my list to return only unique values

//I want my list to return like this.

list={101,zzz;103,aaa};

What I have tried:

have used linq with Distinct() but it gives me non unique field with first occurrence.
Posted
Updated 18-Jan-18 16:35pm
v2
Comments
phil.o 18-Jan-18 20:11pm    
Can you show the definition of your list variable, as well as the linq expression you have tried to use?
Please, do not reply to my comment, rather use the "Improve question" green widget and qualify your question with this relevant information.

The List class does not support multiple dimensions. Read more about it here: <list> (C# Programming Guide) | Microsoft Docs[^]

If you want to use a List class, then you can use a Array, Class, [^]Tuple or KeyValuePair[^] to store your values. For this solution I will use the List<keyvaluepair> as you have non-unique keys. The Dictionary[^] class requires unique keys so is not suitable.
C#
var list = new List<KeyValuePair<int, string>>
{
    new KeyValuePair<int, string> (100, "abc"),
    new KeyValuePair<int, string> (101, "zzz"),
    new KeyValuePair<int, string> (103, "aaa"),
    new KeyValuePair<int, string>(100, "yyy")
};

var results = list.GroupBy(x => x.Key)
                    .Where(x => x.Count() == 1)
                    .Select(x => x.First());

foreach (var item in results)
{
    Console.WriteLine($"Key: {item.Key}  Value: {item.Value}");
}

The Linq expression will group by the Key field, when where there is only 1 unique result per grouping, select the first entry.
 
Share this answer
 
try

List<KeyValuePair<int, string>> lst = new List<KeyValuePair<int, string>>();
        lst.Add(new KeyValuePair<int, string>(100, "abc"));
        lst.Add(new KeyValuePair<int, string>(101, "zzz"));
        lst.Add(new KeyValuePair<int, string>(103, "aaa"));
        lst.Add(new KeyValuePair<int, string>(100, "yyy"));

       
       int[] uniqueItems = lst.GroupBy(k => k.Key).Where(k => k.Count() == 1).Select(k => k.Key).ToArray();
       lst = lst.Where(k => uniqueItems.Contains(k.Key)).ToList();  //list={101,zzz;103,aaa};
 
Share this answer
 
Comments
Graeme_Grant 18-Jan-18 23:40pm    
Almost looks identical...
Karthik_Mahalingam 19-Jan-18 0:34am    
when i was about to post the solution, this thread had no solutions and took some break and posted the solution and i saw your your solution.
however mine is a lengthy process (for better understanding to op) , by getting the unique ids and filtering the main collection.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900