0

I have a function that takes "params string[] requested" as input and is supposed to use readline to get input from the user and see if any match and if anything matches then return what matches

static string inputCheck(params string[] requested){
    string? userInput = Console.ReadLine();
    IEnumerable<string> selected = requested.Where(n => n == userInput);  
    if (selected != null) return selected; //Error is here
    return "Nothing Please Try again";
}

The error I get is "Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'string' (csharp0029)"

I would prefer a solution that uses linq

8
  • If it matter the error is on the word selected Commented Apr 12, 2022 at 21:02
  • 2
    What about if(requested.Any(x => x == userInput)) return userInput;? And FYI Where is never going to return null. Commented Apr 12, 2022 at 21:02
  • selected is a collection of strings. If you want to return a single string, how will you determine which one in the collection to return? Commented Apr 12, 2022 at 21:05
  • You probably want to return selected.First() or .FirstOrDefault() Commented Apr 12, 2022 at 21:07
  • 1
    Where will return them all but it will never be null. It will be empty (which you can check for with if (selected.Any()) return selected) Commented Apr 12, 2022 at 21:15

2 Answers 2

1

You need

static string inputCheck(params string[] requested){
    string? userInput = Console.ReadLine();
    var selected = requested.Where(n => n == userInput).FirstOrDefault(); <<<<===
    if (selected != null) return selected; //Error is here
    return "Nothing Please Try again";
}

Why? Where returns an enumerable of the matches (even if there is only one). FirstOrDefault will return the first one from the list or null if not found

Or even

static string inputCheck(params string[] requested){
    string? userInput = Console.ReadLine();
    var selected = requested.FirstOrDefault(n => n == userInput);  
    if (selected != null) return selected; //Error is here
    return "Nothing Please Try again";
}

given that FirstOrDefault takes an optional predicate

another alternative was

static string inputCheck(params string[] requested){
    string? userInput = Console.ReadLine();
    var selected = requested.Where(n => n == userInput);  
    if (selected.Any()) return selected.First(); 
    return "Nothing Please Try again";
}
Sign up to request clarification or add additional context in comments.

Comments

1

Your method returns string but you return a kind of list inside of your method. Solution:


    public static List<string> InputCheck(params string[] requested)
    {
        Console.Write("search: ");
        var userInput = Console.ReadLine();
        var filteredList = requested
            .Where(n => n == userInput)
            .ToList();

        return filteredList;
    }

..so you can call the method like this:


var yourArray = new[]
{
    "lorem",
    "ipsum",
    "dolor",
    "sit",
    "amet",
    "lorem",
    "ipsum",
    "dolor",
    "sit",
    "amet"
};

var results = YourClass.InputCheck(yourArray);

if(results.Any())
{ 
   foreach (var result in results)
      Console.WriteLine(result);
}


2 Comments

you have enlightened me on some new concepts I could never have known thank you
I updated, check it again pls.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.