I have a String, I wish to use Linq to run a regular expression to cut down my string to a smaller sub-string which matches my reg ex.
My code at the moment gives the error
‘char’ does not contain a definition for ‘Name’ and no extension
method ‘Name’ accepting a first argument of type ‘char’ could be
found
My code :
string variable = result.Name.Select(r => regEx.Match(r.Name).Groups[2].ToString());
Result.Name is a string contained in a custom class.
What have I done incorrectly? What is wrong with my syntax/understanding ?
You’re calling
Selecton a single string.Selectis treating your string as a series of characters, and so therin your lambda expression is achar.If you only have a single string you want to pass into your regular expression, and you only want a single match out, then you don’t need LINQ at all. Just call
(I’m assuming
result.Nameis your single string, based on your example code.)