How to get index from List<string> of specific item?

Michael

I have this List:

List<string> headers = new List<string>(); 
headers.Add("Red"); 
headers.Add("Blue"); 
headers.Add("Green");
headers.Add("Black");

And I have this function:

private int getColorIndex(string colorName , List<string> headers)
{
     int index = LINQ;
}

In getColorIndex function I want to write LINQ that returns the index of specific color from headers list(in colorName variable).

For example if colorName is black the value of index will be 3.

The colorName can contain big or small letters.

For example:

  colorName = Black or colorName = black

Any idea how can I write this LINQ?

Domysee

You don't need LINQ for that. List has an FindIndex method. It takes a predicate and returns the index of the first element that matches. In the predicate you can compare the strings using String.Equals and use a StringComparison parameter to ignore the case.

private int getColorIndex(string colorName , List<string> headers)
{
     int index = headers.FindIndex(s => String.Equals(s, colorName, StringComparison.InvariantCultureIgnoreCase));
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get the last index of a specific item in a List?

From Dev

jQuery - Get index of clicked item from list of elements with specific class

From Dev

How to get the index of a dictionary inside list if a specific key item is in the dictionary

From Dev

remove item from a list by specific index

From Dev

remove item from a list by specific index

From Dev

how to get specific list from database on onclick of a particular item in listview

From Dev

How to get a single String item from List<MyList> Array list

From Dev

How to get characters from a specific index from a string in java

From Dev

How to get characters from a specific index from a string in java

From Dev

Remove item from List<string[]> or get index of any item at any position

From Dev

How to get List index of tapped List item?

From Dev

How to get the index of a specific item in an ArrayList?

From Dev

How to get a specific character from index of a string in swift

From Dev

get item from a list of dictionaries via index

From Dev

How to get a random item from a list, tuple, or string?

From Dev

Linq get specific item from list of lists

From Java

How to get the index of an item in a list in a single step?

From Dev

How to get Specific Array List Item

From Dev

How to get Specific Array List Item

From Dev

How to return item index from list SML?

From Dev

Remove an item at a specific Index from a dynamic String Array

From Dev

Remove an item at a specific Index from a dynamic String Array

From Dev

How can I get a Specific Item Out of a List<Map<String, Object>>

From Dev

Libgdx - How to get a specific item from an array?

From Dev

How to get the index of space before specific string?

From Dev

How to get String with specific index - swift

From Dev

How to get string value of a List<t> Item

From Dev

How do I get an item from an icollection via index when in the class contrusctor I set the icollection as a list?

From Dev

How do I get an item from an icollection via index when in the class contrusctor I set the icollection as a list?

Related Related

  1. 1

    How to get the last index of a specific item in a List?

  2. 2

    jQuery - Get index of clicked item from list of elements with specific class

  3. 3

    How to get the index of a dictionary inside list if a specific key item is in the dictionary

  4. 4

    remove item from a list by specific index

  5. 5

    remove item from a list by specific index

  6. 6

    how to get specific list from database on onclick of a particular item in listview

  7. 7

    How to get a single String item from List<MyList> Array list

  8. 8

    How to get characters from a specific index from a string in java

  9. 9

    How to get characters from a specific index from a string in java

  10. 10

    Remove item from List<string[]> or get index of any item at any position

  11. 11

    How to get List index of tapped List item?

  12. 12

    How to get the index of a specific item in an ArrayList?

  13. 13

    How to get a specific character from index of a string in swift

  14. 14

    get item from a list of dictionaries via index

  15. 15

    How to get a random item from a list, tuple, or string?

  16. 16

    Linq get specific item from list of lists

  17. 17

    How to get the index of an item in a list in a single step?

  18. 18

    How to get Specific Array List Item

  19. 19

    How to get Specific Array List Item

  20. 20

    How to return item index from list SML?

  21. 21

    Remove an item at a specific Index from a dynamic String Array

  22. 22

    Remove an item at a specific Index from a dynamic String Array

  23. 23

    How can I get a Specific Item Out of a List<Map<String, Object>>

  24. 24

    Libgdx - How to get a specific item from an array?

  25. 25

    How to get the index of space before specific string?

  26. 26

    How to get String with specific index - swift

  27. 27

    How to get string value of a List<t> Item

  28. 28

    How do I get an item from an icollection via index when in the class contrusctor I set the icollection as a list?

  29. 29

    How do I get an item from an icollection via index when in the class contrusctor I set the icollection as a list?

HotTag

Archive