I want to rewrite this, maybe using predicates, or Lambda, how can I do this?
serviceResponse = client.Authorize(....);
string authorizeResponse = string.Empty;
foreach (CdcEntry element in serviceResponse.Cdc)
{
authorizeResponse += element.Name + Environment.NewLine;
foreach (CdcEntryItem item in element.Items)
{
authorizeResponse += " (" + item.Key + ", " + item.Value + ") "
+ Environment.NewLine;
}
}
Thanks
I don’t think there’s any real point to use LINQ here, since you’re trying to build a response string, and are thus performing a task most suited to imperative rather than functional programming. (You could of course use the
Aggregatemethod on a string, but that’s not really any help.)In addition, the
StringBuildertype is more suited for this sort of job. (It’s much more efficient, and also cleaner in my opinion. Of course, the former may not matter so much depending on the context.)Here is the optimal way to do this, in my view: