I have a lambda expression and I would like to get back the matched value. Here is an example:
if (Keywords.Any(s => sourceString.Contains(" " + s.Trim())))
return s;
Assuming you only want the first matched result (given you are using Any), you can use First / FirstOrDefault with a filter. Given there is a chance that there could be no matches, I would recommend using FirstOrDefault (First would throw an exception in that case).
var matched = Keywords.FirstOrDefault(s => sourceString.Contains(" " + s.Trim()));
Keywords.First(s => sourceString.Contains(" " + s.Trim()))
Take a look to Single, SingleOrDefault, First or FirstOrDefault extension methods, depending by the behavior you want.
If you want to get the collection of matching items:
Keywords.Where(s => sourceString.Contains(" " + s.Trim())).ToList()
Single / SingleOrDefault here because there is a chance there could be duplicate entries.How about:
return Keywords.FirstOrDefault(s => sourceString.Contains(" " + s.Trim()));
This returns the match if found and null if it wasn't found.
Beware of doing this against a database though - .Contains will likely translate to an inefficient query using LIKE. Investigate full-text indexing if that's the case.
Contains won't even be supported by the provider.