从.NET HashSet的选择通过索引的元素索引、元素、NET、HashSet

2023-09-03 05:20:25 作者:离影

目前我使用的的HashSet 派生的自定义类。还有的时候,我选择在一定条件下项目code点:

At the moment I am using a custom class derived from HashSet. There's a point in the code when I select items under certain condition:

var c = clusters.Where(x => x.Label != null && x.Label.Equals(someLabel));

它工作正常,我得到的那些元素。但有,我可以接受该元素的索引集合中的方法的ElementAt来使用,而不是整个对象,一个方法是什么?

It works fine and I get those elements. But is there a way that I could receive an index of that element within the collection to use with ElementAt method, instead of whole objects?

它看起来或多或少是这样的:

It would look more or less like this:

var c = select element index in collection under certain condition;
int index = c.ElementAt(0); //get first index
clusters.ElementAt(index).RunObjectMthod();

时手动遍历整个集合更好的办法?我要补充一点,它是在一个更大的循环,所以这个其中,子句对于不同的 someLabel 字符串进行多次。

Is manually iterating over the whole collection a better way? I need to add that it's in a bigger loop, so this Where clause is performed multiple times for different someLabel strings.

修改

我需要这个? 集群是一组集群的一些文件收集的。文件分为簇按主题相似。所以该算法的最后步骤之一是发现标签为每个群集。但算法是不完美的,有时它使两个或多个集群相同的标签。我想要做的就是简单地合并这些集群到大的。

What I need this for? clusters is a set of clusters of some documents collection. Documents are grouped into clusters by topics similarity. So one of the last step of the algorithm is to discover label for each cluster. But algorithm is not perfect and sometimes it makes two or more clusters with the same label. What I want to do is simply merge those cluster into big one.

推荐答案

设定不一般的有无的索引。如果位置对你很重要,你应该使用名单,其中,T> ,而不是(或可能还有)一组

Sets don't generally have indexes. If position is important to you, you should be using a List<T> instead of (or possibly as well as) a set.

现在 的SortedSet&LT; T&GT; 在.NET 4略有不同,因为它保持排序的价值秩序。但是,它仍然不执行的IList&LT; T&GT; ,所以获得通过指数与的ElementAt 将是缓慢的

Now SortedSet<T> in .NET 4 is slightly different, in that it maintains a sorted value order. However, it still doesn't implement IList<T>, so access by index with ElementAt is going to be slow.

如果你能给你为什么想要这个功能的详细信息,这将有助于。您的使用情况并不真的清楚的时刻。

If you could give more details about why you want this functionality, it would help. Your use case isn't really clear at the moment.