tags:

views:

2627

answers:

6

Hi,

I got a list<list<string>>

in list[x][0] are records from which I want to choose unique records thus such record wouldn't be in any other list[x][0], when I choose it, i'd like whole row list[x] to be chosen. I haven't found the appropriate exapmple for this in Linq, please help :(

EDIT

When Jon Skeet asks me to clarify, I can't deny ;-)

list<list<string>>

contains list of string table . Each of the string "table" contains several keys list[x][several_items] and I want to get unique records from list-> meaning FIRST item in that "table".

Thus:

item[0] = "2","3","1","3"
item[1] = "2","3","4","2"
item[3] = "10","2"
item[4]= "1","2"

-> unique would mean that I can derive rows item[3] and item[4] as unique. because first occurence of number/string is important.

If there are 2 or more records/rows (item[x] of which first item (item[x][0]) exists more than once in the list, it's not unique.

First element of each list is important to determine uniqueness. Maybe it'd be easier if someone can help to find a way to find non-unique -> so from the above example the list I'd get only item[0] and item[1]

+4  A: 

EDIT: I've updated the UniqueBy implementation at the bottom to be significantly more efficient, and only iterate through the source once.

If I've understood you correctly (the question is pretty unclear - it would really help if you could provide an example) this is what you want:

public static IEnumerable<T> OnlyUnique<T>(this IEnumerable<T> source)
{
    // No error checking :)

    HashSet<T> toReturn = new HashSet<T>();
    HashSet<T> seen = new HashSet<T>();

    foreach (T element in source)
    {
        if (seen.Add(element))
        {
            toReturn.Add(element);
        }
        else
        {
            toReturn.Remove(element);
        }
    }
    // yield to get deferred execution
    foreach (T element in toReturn)
    {
        yield return element;
    }
}

EDIT: Okay, if you only care about the first element of the list for uniqueness, we need to change it somewhat:

public static IEnumerable<TElement> UniqueBy<TElement, TKey>
    (this IEnumerable<TElement> source,
     Func<TElement, TKey> keySelector)
{
    var results = new LinkedList<TElement>();
    // If we've seen a key 0 times, it won't be in here.
    // If we've seen it once, it will be in as a node.
    // If we've seen it more than once, it will be in as null.
    var nodeMap = new Dictionary<TKey, LinkedListNode<TElement>>();

    foreach (TElement element in source)
    {
        TKey key = keySelector(element);
        LinkedListNode<TElement> currentNode;

        if (nodeMap.TryGetValue(key, out currentNode))
        {
            // Seen it before. Remove if non-null
            if (currentNode != null)
            {
                results.Remove(currentNode);
                nodeMap[key] = null;
            }
            // Otherwise no action needed
        }
        else
        {
            LinkedListNode<TElement> node = results.AddLast(element);
            nodeMap[key] = node;
        }
    }
    foreach (TElement element in results)
    {
        yield return element;
    }
}

You'd call it with:

list.UniqueBy(row => row[0])
Jon Skeet
yes I want only those that occur only once
Skuta
I re-stated my question with more info
Skuta
Hmm. I'm afraid I still don't understand. Do you only care about the first element of each list? Editing to try that...
Jon Skeet
new info in post :( sorry
Skuta
Edited. Hopefully this is what you want now, and efficiently.
Jon Skeet
+1  A: 

Something like this, perhaps?

I'm now fairly sure this would work for you, given your clarification :)

var mylist = new List<List<string>>() {
    new List<string>() { "a", "b", "c" },
    new List<string>() { "a", "d", "f" },
    new List<string>() { "d", "asd" },
    new List<string>() { "e", "asdf", "fgg" }
};
var unique = mylist.Where(t => mylist.Count(s => s[0] == t[0]) == 1);

unique now contains the "d" and "e" entries from above.

Blorgbeard
I think you got it. I'll check it ! :)
Skuta
is there any chance to reserve it? so I'm looking for a,a entries? :D
Skuta
Probably.. Can you explain that a bit more? :)
Blorgbeard
A: 

You could maintain a list and an index/dictionary:

List<List<string>> values;
Dictionary<string, List<string>> index;

When you add an item to values, you also add the List to the index with the string as index.

values[x].Add(newString);
index[newString] = values[x];

Then you can get the correct list by:

List<string> list = index[searchFor]

You loose some (minimal) performance and memory when building the index, but you gain a lot when retrieving the data.

If the string is not unique, you could also store a List> in the dictionary/index, to allow multiple results per index key.

Sorry no Linq, this doesn't look that cool, but you have a fast lookup, and IMHO the lookup code is more clear.

GvS
can you tell me more about this approach? it looks cool.
Skuta
A: 

I'll just go ahead and add this one to the fray.

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication1 {
    class Program {
        static void Main(string[] args) {
            List<string> xx = new List<string>() { "xx", "yy", "zz" };
            List<string> yy = new List<string>() { "11", "22", "33" };
            List<string> zz = new List<string>() { "aa", "bb", "cc" };
            List<List<string>> x = new List<List<string>>() { xx, yy, zz, xx, yy, zz, xx, yy };
            foreach(List<string> list in x.Distinct()) {
                foreach(string s in list) {
                    Console.WriteLine(s);
                }
            }
        }
    }
}
Ariel
According to my knowledge of linq (almost none actually) this is not right. please check my post again :(
Skuta
Well with the new info in your post, it seems to me that you are using the wrong structure to hold your data. you may be better off with a hashtable or datatable.
Ariel
actually, now that i read more just a straight up array.
Ariel
A: 

Here's some Linq for you.

List<List<string>> Records = GetRecords();
//
List<List<string> UniqueRecords = Records
  .GroupBy(r => r[0])
  .Where(g => !g.Skip(1).Any())
  .Select(g => g.Single())
  .ToList();
David B
A: 

Hi,

Here is the code you need. It works perfectly for me to select ONLY distinct values.

//distinct select in LINQ to SQL with Northwind

var myquery = from user in northwindDC.Employees

          where user.FirstName != null || user.FirstName != ""

      orderby user.FirstName

      group user by user.FirstName into FN

      select FN.First();

Huseyin Altindag

Huseyin Altindag