1

I have a query where it reverse the characters and capitalize the first letter after the reversed. In my T-SQL, it's working but when I'm trying to convert it to Linq query, I'm having a issues that says

Reverse is not supported

Here's my working query

Upper(Left(REVERSE(Firstname),1))+Lower(Reverse(Left(Right(Firstname, LEN(Firstname)),LEN(Firstname)-1))) as NameReverse

// "Name" is the result without reverse, but after the reverse query, it will be
// "Eman"

Here's my reverse in linq that doesn't work

TheLengthOfName = name.FirstName.Reverse()

Thank you in advance.

spidy787
  • 43
  • 6
  • [This might help.](http://stackoverflow.com/questions/228038/best-way-to-reverse-a-string) Also, your sql is over-complicated. This works the same: `UPPER(LEFT(REVERSE(FirstName), 1)) + LOWER(RIGHT(REVERSE(FirstName), LEN(Firstname) - 1))` – Zohar Peled Apr 15 '17 at 06:11
  • Hi @ZoharPeled, how can I achieve it on LinQ? :) – spidy787 Apr 15 '17 at 06:20
  • You could use one of the solutions offered in that post to create an extension method for string reverse. – Zohar Peled Apr 15 '17 at 06:29
  • Oh, do you have a sample code? Thank you – spidy787 Apr 15 '17 at 06:35
  • Sample code of what? creating extension methods? – Zohar Peled Apr 15 '17 at 06:39
  • No, sample code to convert UPPER(LEFT(REVERSE(FirstName), 1)) + LOWER(RIGHT(REVERSE(FirstName), LEN(Firstname) - 1)) to linq. – spidy787 Apr 15 '17 at 06:52
  • You don't need linq for that, you simply need to create 2 extension methods for strings - one for reverse and one for [capitalizing](http://stackoverflow.com/questions/4135317/make-first-letter-of-a-string-upper-case-with-maximum-performance). After you have these methods all the rest is just as normal. – Zohar Peled Apr 15 '17 at 07:45
  • But my list of names are derive from table with a column of Firstname. That's why i need to return a linq query of ReverseName column. – spidy787 Apr 15 '17 at 07:51
  • `var x = name.FirstName.Reverse().Capitalize()` – Zohar Peled Apr 15 '17 at 08:13
  • @ZoharPeled I am looking for C# expression not C# statement :( – spidy787 Apr 15 '17 at 09:47

1 Answers1

0

Thanks to R. Martinho Fernandes that provided this answer And to RobinHood70 that offered an improvement, I created an extension method for string reverse:

public static string Reverse(this string s)
{
    if(string.IsNullOrEmpty(s))
    {
        return s;
    }
    var info = new StringInfo(s);
    var sb = new StringBuilder();

    for(int i = info.LengthInTextElements - 1; i > -1 ; i--)
    {
        sb.Append(info.SubstringByTextElements(i, 1));
    }
    return sb.ToString();
}

I've slighly modified Darren Sherwood's answer here, to return the capitalized string you wanted:

public static string Capitalize(this string s)
{
    if(string.IsNullOrEmpty(s))
    {
        return s;
    }
    var charArray = s.ToLower().ToCharArray();
    charArray[0] = Char.ToUpper(charArray[0]);
    return new string(charArray);
}

Now, to combine it with a linq query you need something like this:

var query = from person in people
            select person.FirstName.Reverse().Capitalize();

See a live demo on rextester.

Community
  • 1
  • 1
Zohar Peled
  • 79,642
  • 10
  • 69
  • 121