0

I have if statement like this:

if (myList.Any(x => s.Contains(x))
{
//some code here
}

in which I check if there is a string in myList which is contained in a string s. Is it somehow possible to get the element x of this list and use it in the if statement showed above (in "//some code here" part), when the condition is met?

Thank you.

2
  • will myList contain null ever? Commented Jun 14, 2016 at 21:51
  • no, it wiil not contain null Commented Jun 14, 2016 at 21:57

2 Answers 2

4

Switch from Any to FirstOrDefault, that will return the item that matched the test or null if no item was matched.

var found = myList.FirstOrDefault(x => s.Contains(x));
if (found != null)
{
//some code here
}

If null could be considered a "valid value" for a element in myList you can create a extension method TryFirst

public static class ExtensionMethods
{
    public static bool TryFirst<T>(this IEnumerable<T> @this, Func<T, bool> predicate, out T result)
    {
        foreach (var item in @this)
        {
            if (predicate(item))
            {
                result = item;
                return true;
            }
        }
        result = default(T);
        return false;
    }
}

This would let you do

string foundString;
var wasFound = myList.TryFirst(x => s.Contains(x), out foundString);
if (wasFound)
{
//some code here
}

and tell the difference between a null in your list and a default result.

The above two methods only act on the first item on the list that the Contains will match, if you want to act on all the items use a Where( clause and a foreach

foreach(var item in myList.Where(x => s.Contains(x))
{
//some code here
}

You must promise you will not use the following code and use one of the other options first

You can also do your stated question, it is possible to get a variable assigned to inside lambada. However this can not be done with expression lambadas, only with statement lambadas.

string matachedString = null;
if (myList.Any(x => { var found = s.Contains(x);
                      if(found)
                          matachedString = x;
                      return found;
                     });
{
//some code here
}

But only do this option as a last resort, use one of the more appropriate methods like FirstOrDefaut or write a custom method like TryFirst first.

Sign up to request clarification or add additional context in comments.

3 Comments

I am not sure why, but when I use FirstOrDefault you've suggested, it only works when s is equal to x. For example, if s="email", and x="email", it works, but it doesn't work when s="emailaaa" and x="email".
I would double check that s and x are what you expect them to be inside the debugger. if you switch to myList.Any(x => { var found = s.Contains(x); return found; }); you can put a breakpoint on the return and see the value for s, x and found.
I found what was the problem. Your solution is OK... There was something in my code causing this behavior (it's not related to the topic, so I will not explain what was the problem).
1

I'd use foreach/Where()for this, even if I'm only expecting 0 or 1 result:

foreach (var item in myList.Where(x => s.Contains(x)))
{
    //some code here
}

Comments

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.