Thursday, September 6, 2012

LINQ .ToArray().Aggregate() Function

Method 1 (OLD):
var stringVar = new StringBuilder();
foreach (var item in itemsCollection)
{
    stringVar.Append(item.PROPERTY + ", ");
}
if (codes.Length > 0)
{
    stringVar.Remove(item.PROPERTY - 2, 2);
}
else
{
    stringVar.Append("None");
}
return stringVar.ToString();

Method 2 (PREFERRED):
return itemsCollection.Any() ? itemsCollection.ToArray().Aggregate((first, second) => first + ", " + second) : "None";

No comments: