Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a condition in a LINQ statement

Tags:

c#

linq

I've a C# statement as follows:

var errors =  errorList.Select((e, i) => string.Format("Error occured #{0}: {1} (Error code = {2}).", i + 1, e.Message, e.ErrorCode)).ToArray();

I need to display "Error occured" when e.ErrorCode is 'Error' and "Warning occured" when e.ErrorCode is 'Warning'. How do I add this condition to the above statement please?

Thanks.

like image 795
Jimmy Avatar asked Jan 22 '26 02:01

Jimmy


1 Answers

Couldn't you just do this:

errorList.Select((e, i) => string.Format("{2} Occured #{0}: {1} (Error code = {2}).", i + 1, e.Message, e.ErrorCode)).ToArray();
like image 65
JMK Avatar answered Jan 24 '26 14:01

JMK