Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3234986
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:27:17+00:00 2026-05-17T17:27:17+00:00

I have a string array: string[] Animals = {Cat, Dog, Fish}; I then want

  • 0

I have a string array:

string[] Animals = {"Cat", "Dog", "Fish"};

I then want to determine which element contains the sequence “is” and return that entire element; in this case “fish”

If I want to find “gh”, it does not exist in the list, so it should return the first element, in this case “Cat”

I’ve tried this linq code, but i don’t think I’m doing the lambda part right.

int index = Animals.Where(x => x.IndexOf("is") >= 0).First().IndexOf("is")
string result = index > 0 ? Animals[index] : Animals[0];

This code throws this error:

Exception Details: System.ArgumentNullException: Value cannot be null.
Parameter name: value

I think I’m close, I just can’t seem to get it.

This method obviously isn’t fool proof, it should return the first instance of “is” which could be problematic. My potential list is fairly small and the index word is always unique.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T17:27:18+00:00Added an answer on May 17, 2026 at 5:27 pm

    Try this:

    string result = Animals.FirstOrDefault(x => x.Contains("is")) ?? Animals.First();
    

    (This will fail if the array contains no elements; what do you want to do in this case? You could try FirstOrDefault for the fallback expression as well – this will return null if the sequence is empty.)

    Given your requirements, the code you posted has 2 issues:

    1. It uses Enumerable.First, which will throw an exception on an empty sequence i.e. if no item exists that matches the original predicate.
    2. The index you are using in the the second statement is the index of the “is” substring in the result of the first query, not the index of the result in the original array. Consequently, it does not make sense to use that number to index the original array.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.