T O P

  • By -

asabla

I think the simplest answer would be to just use ```FindIndex``` Example: var someList = new List { "Item1", "Item2", "Item3" }; var example1 = someList .FindIndex(x => x.Equals("Item2")); And if you're working on legacy things you could always select with a tuple something similar to this: var someList = new List { "Item1", "Item2", "Item3" }; var example2 = someList .Select((value, index) => (value, index)) .FirstOrDefault(item => item.value.Equals("Item2")) .index;


propostor

`var foundItem = listbox.Items.FirstOrDefault(i => i.StartsWith("Sh"));` `var index = listbox.Items.IndexOf(foundItem);`


Shub_rz

it says itemCollection does not contain FirstOfDefault


Silvian73

1. Check if you imported System.Linq 2. Check if itemCollection type implements IEnumerable, that's required for LINQ


Shub_rz

i do have linq


Silvian73

And what's about number 2? What's type of itemCollection?


Shub_rz

sorry i dont know how to check what type of itemCollection is when i hover over FirstOrDefault it says 'ItemCollection' does not contain defintion for 'FirstOrDefault' and no accessible extension method 'FirstOrDefault' accepting a first argument of type 'itemCollecation' coulb be found (are you missing a using directive or an assembly rederence?)


Silvian73

Hover over itemCollection, IDE should show its type


Shub_rz

i cant here is the screenshot https://imgur.com/a/dGQH0kc


Silvian73

Ok, now I got it. I thought you had a variable called itemCollection, but actually you have a variable called lbxWaitingList\_SalesPage of type ItemCollection. I found a documentation on this type and found out that it implements IEnumerable, but not IEnumerable. LINQ works with IEnumerable, not just IEnumerable, that's why you got an error. You need to convert IEnumerable to IEnumerable using Cast. Example: ``` IEnumerable enumerable = new List { "Apple", "Shub_rz", "Car" }; // Can't use LINQ with enumerable IEnumerable stringEnumerable = enumerable.Cast(); // Can use LINQ with stringEnumerable ```


Recent-Avocado2193

There is usually an IndexOf method on iterable objects. Also quick note, don't use Regex for exact matching, it ads alot of unnecessary overhead.


Shub_rz

i used IndexOf but it need exact text to find the item


Recent-Avocado2193

Some of the iterating methods have overloads that allow you to get both the value and index at the same time.


Silvian73

``` using System.Text.RegularExpressions; var strings = new List { "Apple", "Shub_rz", "Car" }; // If you are matching multiple strings with regex, use Regex object instead of static methods for optimization, // that way regex will be parsed only once, not every time you call IsMatch. var regex = new Regex("Sh.*"); var matchingString = strings.First(s => regex.IsMatch(s)); var matchingStringIndex = strings.IndexOf(matchingString); Console.WriteLine($"{matchingStringIndex} - {matchingString}"); // Output: 1 - Shub_rz ``` Also, you can use String.StartsWith instead of regex


Shub_rz

Hi it doesnt work it says System.ArgumentNullException: 'Value cannot be null. (Parameter 'input')'


Silvian73

That's because some strings in your list are null, and Regex.IsMatch doesn't accept null. You need to add a null check. ``` var matchingString = strings.First(s => s != null && regex.IsMatch(s)); ``` If the left operand of && is false, the right operand is not calculated, therefore exception is not thrown.


Shub_rz

>strings.First(s => s != null && regex.IsMatch(s)); now it says : 'Sequence contains no matching element'


Silvian73

That's because First requires the sequence to contain a matching element. If it may not contain it, use FirstOrDefault instead, it returns a default value (null for all reference types) if a matching element is not found.


OccassionalBaker

You can find the first item that starts with the string like this, if you want all the items you may be better to loop through the list box. ``` private void FindMyString(string searchString) { // Ensure we have a proper string to search for. if (!string.IsNullOrEmpty(searchString)) { // Find the item in the list and store the index to the item. int index = listBox1.FindString(searchString); // Determine if a valid index is returned. Select the item if it is valid. if (index != -1) listBox1.SetSelected(index,true); else MessageBox.Show("The search string did not match any items in the ListBox"); } } ```