简体   繁体   English

C#:从列表中选择特定的索引值

[英]C#: Selecting particular index values from a list

I am looking for a way to search a list that returns me an bunch of indexes based on a search i make on that list.我正在寻找一种搜索列表的方法,该列表根据我在该列表上进行的搜索返回一堆索引。 For ex i have a comma separated string list as follows:例如,我有一个逗号分隔的字符串列表,如下所示:

Blue, 3
Red, 3
Blue, 1
Blue, 9
Red, 5

I want to make a search that returns me indexes of all elements EXCEPT any that contain the text found in a criteria list.我想做一个搜索,返回所有元素的索引,除了包含在条件列表中找到的文本的任何元素。 The criteria list could contain:标准列表可能包含:

Blue, 3
Red, 5

So in pseudo code, it would be,所以在伪代码中,它会是,

ColorList.SelectIndex(!Containing(Words found in all elements of criteriaList) ColorList.SelectIndex(!Containing(在criteriaList的所有元素中找到的词)

The above should return indexes 1,2,3以上应返回索引 1,2,3

Thanks谢谢

var indexes = ColorList.Select((x, i) => new { Value = x, Index = i })
                       .Where(x => !criteriaList.Contains(x.Value))
                       .Select(x => x.Index);

If your lists contain many items then you might get better performance by converting criteriaList to a HashSet<T> first.如果您的列表包含许多项目,那么您可以通过先将criteriaList转换为HashSet<T>来获得更好的性能。 (You'll need to benchmark to determine whether this is the better option in your case.) (您需要进行基准测试以确定这是否是您情况下的更好选择。)

var criteriaSet = new HashSet<string>(criteriaList);
var indexes = ColorList.Select((x, i) => new { Value = x, Index = i })
                       .Where(x => !criteriaSet.Contains(x.Value))
                       .Select(x => x.Index);
var list = new []{"Blue, 3", "Red, 3", "Blue, 1", "Blue, 9", "Red, 5"};
var criteria = new []{"Blue, 3", "Red, 5"};

var filtered = list
        .Select((s,i)=>new {s,i})
        .Where(e => !criteria.Contains(e.s))
        .Select(e => e.i);

results: { 1, 2, 3 }结果: { 1, 2, 3 }

void Main()
{
    var data = new List<string> {"Blue, 3", "Red, 3", "Blue, 1", "Blue, 9", "Red, 5"};

    var colorList = new List<string> {"Blue, 3", "Red, 5"};

    var indexes = data.Except(colorList).Select (x => data.IndexOf(x));

    indexes.Dump();
}

Sounds like you may want to use a听起来你可能想使用

 List<KeyValuePair<string, int>> pair = new KeyValuePair<string, int>

then you can do那么你可以做

foreach (KeyValuePair<string, int> p in pair){
    s = p.Key;
    i = p.Value;
}

to get the values获取值

I am not quite sure what you want to achieve here, so perhaps my answer is not what you are looking for.我不太确定你想在这里实现什么,所以也许我的答案不是你想要的。

Anyway, in a generic list, you could use a lambda expression/ linq statement on your collection.无论如何,在通用列表中,您可以在您的集合上使用 lambda 表达式/ linq 语句。 Consider these examples I have written for you:考虑一下我为你写的这些例子:

internal class ListLambdaLINQSample
{

    List<KeyValuePair<Colors, int>> listSource;
    List<KeyValuePair<Colors, int>> listCriteria;
    List<KeyValuePair<Colors, int>> listMatches;

    private const int COLORCODE1 = 1;
    private const int COLORCODE2 = 2;
    private const int COLORCODE3 = 3;
    private const int COLORCODE4 = 4;
    private const int COLORCODE5 = 5;

    internal enum Colors
    {
        Red, Blue, Green, Yellow
    }


    public ListLambdaLINQSample()
    {   // populate the list
        listSource = new List<KeyValuePair<Colors, int>>();
        listCriteria = new List<KeyValuePair<Colors, int>>();

        _populateListCriteria();
        _populateListSource();

        ...
    }

    private void _getMatchesWithLINQ()
    {
        listMatches =
                        (from kvpInList
                             in listSource
                         where !listCriteria.Contains(kvpInList)
                         select kvpInList).ToList();
    }

    private void _getMatchesWithLambda()
    {
        listMatches =
            listSource.Where(kvpInList => !listCriteria.Contains(kvpInList)).ToList();
    }


    private void _populateListSource()
    {
        listSource.Add(new KeyValuePair<Colors, int>(Colors.Blue, COLORCODE1));
        listSource.Add(new KeyValuePair<Colors, int>(Colors.Green, COLORCODE2));
        listSource.Add(new KeyValuePair<Colors, int>(Colors.Red, COLORCODE3));
        listSource.Add(new KeyValuePair<Colors, int>(Colors.Yellow, COLORCODE4));
    }

    private void _populateListCriteria()
    {
        listCriteria.Add(new KeyValuePair<Colors, int>(Colors.Blue, COLORCODE1));
        listCriteria.Add(new KeyValuePair<Colors, int>(Colors.Green, COLORCODE2));
    }
}

Hope this helps!!希望这可以帮助!!

Regards, Nico问候,尼科

PS: I haven't compiled nor tested this code. PS:我没有编译也没有测试过这段代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM