question

GunjanArora-8600 avatar image
0 Votes"
GunjanArora-8600 asked TimonYang-MSFT commented

How can I achive this in Lambda expression ?

Hi Team,

I have following lambda expression

public string Result=> ResultAbbreviation?.Replace("REG:","")
.Replace("Exc","Excellent")
.Replace("Out","Outstanding")
.Replace("Avg","Average")

Now I have my ResultAbbreviation can be of following values REG:Exc,REG:Out,REG: Avg also there can be values like EXREG:Exc,EXREG:Out,EXREG:Avg

What I want to achieve is if My value of ResultAbbreviation contains EXREG I want the Result to be displayed as EX Regular Excellent,EX Regular Outstanding,EX Regular Average but if the ResultAbbreviation contains REG then I want to display the value as Excellent,Outstanding,Average

How can I achieve it in the existing lambda expression ?

dotnet-csharp
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

You should design a lookup table that maps the abbreviates to the full descriptions. Then there is no need to replace strings. I'm not a bit fan of hardcoded string in C# code and would fail this design in a code review.

0 Votes 0 ·

Please see my latest comment as I was wrong about the requirement .

0 Votes 0 ·

1 Answer

Viorel-1 avatar image
0 Votes"
Viorel-1 answered TimonYang-MSFT commented

Try a solution that does not use lambda expressions:

 public string Result => ResultAbbreviation?.Replace( "EXREG:", "EX Regular " ).Replace( "REG:", "" )
    .Replace( "Exc", "Excellent" )
    .Replace( "Out", "Outstanding" )
    .Replace( "Avg", "Average" );


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi Viorel,

Sorry , I was wrong about the input values

The values of ResultAbbreviation can be of following values REG:Exc,REG:Out,REG: Avg or there would be values like Exc,Out,Avg

What I wanted is if the ResultAbbreviation starts with REG I want to display it as Excellent,Outstanding ,Average
else if the ResultAbbreviation does not contain REG then it should append Ex Regular Excellent ,Ex Regular Outstanding,Ex Regular Average


0 Votes 0 ·

In this case, the most appropriate way is to use if-else.

         if (ResultAbbreviation.StartsWith("REG:"))
         {
             ...
         }
         else
         {
             ...
         }

This way needs to be done in one method.

0 Votes 0 ·