有机会使用Linq(C#)获得唯一记录吗? [英] Any chance to get unique records using Linq (C#)?
问题描述
我得到了list<list<string>>
是我要从中选择唯一记录的记录,因此,当我选择该记录时,我希望选择整行list[x]
.我在Linq中找不到适合的示例,请帮助:(
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 :(
编辑
当乔恩·斯基特(Jon Skeet)要求我澄清时,我不能否认;-)
When Jon Skeet asks me to clarify, I can't deny ;-)
list<list<string>>
包含字符串表列表.每个字符串"table"都包含多个键list[x][several_items]
,我想从list->获取唯一记录,这意味着该"table"中的FIRST项目.
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".
因此:
item[0] = "2","3","1","3"
item[1] = "2","3","4","2"
item[3] = "10","2"
item[4]= "1","2"
-> unique意味着我可以派生item[3] and item[4]
行为唯一.因为数字/字符串的首次出现很重要.
-> unique would mean that I can derive rows item[3] and item[4]
as unique. because first occurence of number/string is important.
如果有2个或更多记录/行(item[x] of which first item (item[x][0])
在列表中存在多次,则不是唯一的.
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.
每个列表的第一个元素对于确定唯一性都很重要.如果有人可以帮助找到一种查找非唯一性的方法,也许会更容易->因此,从上面的示例中,列表中我只会看到item [0]和item [1]
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]
推荐答案
我已经更新了底部的UniqueBy
实现,使其效率大大提高,并且仅遍历了源代码一次.
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;
}
}
好的,如果您只关心列表的第一个元素的唯一性,我们需要对其进行一些更改:
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])
这篇关于有机会使用Linq(C#)获得唯一记录吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!