Just sharing some of my inconsequential lunch conversations with you... RSS  

Monday, February 16, 2009

Using LINQ’s aggregate

A long time ago, on a far away galaxy, every time we fell need to aggregate let’s say a string of IPs comma separated, we had to write something like:

                string ips = String.Empty;

foreach (var address in ipInterfaceProperties.UnicastAddresses)
if (ips != String.Empty)
ips += "; ";

ips += address.Address.ToString();

With a more functional approach we can now write it like:

                string ips = 
.Select(p => p.Address.ToString())
.Aggregate((acumulator, next) => acumulator + "; " + next);

Please, God, don’t ever throw me into a non LINQ project anymore, please!…

No comments:

Development Catharsis :: Copyright 2006 Mário Romano