Just put this in a little demo to make it clear. I created a simple object Person, with ID, FirstName and LastName.
Now I fill a List with 3 persons (later I'll fill it with 20000 to do a performance check):
List<Person> persons = newList<Person>();
{
newPerson(){ID=0, FirstName="Mark", LastName="Deraeve"},
newPerson(){ID=1, FirstName="Priscilla", LastName="Lauwerijssen"},
newPerson(){ID=2, FirstName="Anne-Lisa", LastName="Deraeve"}
};
Instead of looping over the list and concatinating it, like this:
start = DateTime.Now;
foreach (Person p in persons)
{
sPersons = String.Format("{0},{1}", sPersons, p.FirstName);
}
//remove front ,
Console.WriteLine("With Loop: " + sPersons);
I can also just use 1 line to accomplisch this:
Console.WriteLine("With LINQ: " +string.Join(",", persons.Select(p => p.FirstName)));
Result:

Now let's say you do not want all records, this makes it even more easy, just put a Where before the Select and you got it!
Let's only show person named 'Deraeve':
Console.WriteLine("With LINQ: " +string.Join(",", persons.Where(p => p.LastName == "Deraeve").Select(p => p.FirstName)));
Result:

Now last, lets try to see performance difference. I'll fill the list with 20000 persons. This is where the power comes in. (The number 188889 or 188890(+1 for the extra comma) is the length of the result string)

Amazingly, the LINQ only uses 3 or 4 milliseconds, the old for loop takes 1-3 (There is a difference when using String.Format (slower) or just "string + ','+ "string") seconds. This can really make a big difference in today's apps.
So here the complete code so you can test it yourself:
Code of the console app:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TestConsoleApp
{
classProgram
{
staticvoid Main(string[] args)
{
List<Person> persons = newList<Person>();
/*{
new Person(){ID=0, FirstName="Mark", LastName="Deraeve"},
new Person(){ID=1, FirstName="Priscilla", LastName="Lauwerijssen"},
new Person(){ID=2, FirstName="Anne-Lisa", LastName="Deraeve"}
};*/
for (int i = 0; i < 20000; i++)
{
persons.Add(newPerson() { ID = i, FirstName = string.Format("Name{0}", i), LastName = "Deraeve" });
}
string sPersons="";
DateTime start = DateTime.Now;
TimeSpan ts;
start = DateTime.Now;
sPersons = string.Join(",", persons.Select(p => p.FirstName));
ts = DateTime.Now.Subtract(start);
Console.WriteLine("With LINQ: " + sPersons.Length);
Console.WriteLine(String.Format("Seconds:{0} Miliseconds{1} ", ts.Seconds, ts.Milliseconds));
Console.WriteLine("");
sPersons = "";
start = DateTime.Now;
sPersons = string.Join(",", persons.Where(p => p.LastName == "Deraeve").Select(p => p.FirstName));
ts = DateTime.Now.Subtract(start);
Console.WriteLine("With LINQ and Where: " + sPersons.Length);
Console.WriteLine(String.Format("Seconds:{0} Miliseconds{1} ", ts.Seconds, ts.Milliseconds));
Console.WriteLine("");
sPersons="";
start = DateTime.Now;
foreach (Person p in persons)
{
sPersons = String.Format("{0},{1}", sPersons, p.FirstName);
}
ts = DateTime.Now.Subtract(start);
Console.WriteLine("With for loop: " + sPersons.Length);
Console.WriteLine(String.Format("Seconds:{0} Miliseconds{1} ",ts.Seconds,ts.Milliseconds));
Console.WriteLine("");
sPersons = "";
start = DateTime.Now;
foreach (Person p in persons)
{
if (p.LastName == "Deraeve")
{
sPersons = String.Format("{0},{1}", sPersons, p.FirstName);
}
}
ts = DateTime.Now.Subtract(start);
Console.WriteLine("With for loop and if: " + sPersons.Length);
Console.WriteLine(String.Format("Seconds:{0} Miliseconds{1} ", ts.Seconds, ts.Milliseconds));
Console.WriteLine("");
sPersons = "";
start = DateTime.Now;
foreach (Person p in persons)
{
sPersons = sPersons + ',' + p.FirstName;
}
ts = DateTime.Now.Subtract(start);
Console.WriteLine("With for loop without format: " + sPersons.Length);
Console.WriteLine(String.Format("Seconds:{0} Miliseconds{1} ", ts.Seconds, ts.Milliseconds));
Console.WriteLine("");
Console.ReadKey();
}
}
}
Code of the Person class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TestConsoleApp
{
publicclassPerson
{
publicstring FirstName { get; set; }
publicstring LastName { get; set; }
publicint ID { get; set; }
}
}
Things even get better for the Join statement when we add the last name to the result:
sPersons = string.Join(",", persons.Where(p => p.LastName == "Deraeve").Select(p => p.FirstName + ' ' + p.LastName));
VS
foreach (Person p in persons)
{
sPersons = String.Format("{0},{1} {2}", sPersons, p.FirstName, p.LastName);
}
Result:
