Q:

replace List.foreach to LINQ

I'm new to LINQ and doing some experiments with it.
Sorry if it is a duplicate but I cant seem to find proper guide (for me) to it
I want to replace this code : 
DataTable table
List<string> header = new List<string>();
table.Columns.Cast<DataColumn>().ToList().ForEach(col => header.Add(col.ColumnName));

with something LINQ like:
var LINQheader = from mycol in table.Columns select mycol.ColumnName;
LINQheader.tolist();

but it doesn't even compile.
what I want Is not a one line solution but would like some logic to understand how construct it with more complicated environments (Like choosing many node in XML with some logic)   

A:

You can use Enumerable.Aggregate() for this:
var header = table.Columns.Cast<DataColumn>().Aggregate(new List<string>(), (list, col) => { list.Add(col.ColumnName); return list; });

In general, Linq allows for retrieval and transformation of sequences of data from data sources.  What you want to do in this question is to iterate over a sequence and return an immediate result.  That isn't the primary focus of Linq, but there are methods that perform tasks like this, including Aggregate(), Average(), Count(), Max() and so on.

A:

here is the original code
table.Columns.Cast<DataColumn>().ToList().ForEach(col => header.Add(col.ColumnName));

Why Cast used?
because it allows you to treat DataColumnCollection items as a DataColumn not an object.
Why ToList used?
becuase it converts your IEnumerable to List and allows you to call ForEach because this function is special method that exists in List class.
Why ForEach used?
because it allows you to do what you want for each element on the list (in your case it adds column name of each column to another list(header)).
Simplified version:
now assume you want to add column names to header where they starts with "Student"
you can write something like this
DataTable table = new DataTable();
List<string> header = new List<string>();

foreach (DataColumn col in table.Columns)
{
    if (col.ColumnName.StartsWith("Id")) // you can remove this line if you want to add all of them
       header.Add(col.ColumnName);
}

you can also use this
table.Columns.Cast<DataColumn>()
    .ToList()
    .ForEach(col =>
    {
        if (col.ColumnName.StartsWith("Id"))
            header.Add(col.ColumnName)
    });

or 
var headers = table.Columns.Cast<DataColumn>()
        .Where(col => col.ColumnName.StartsWith("Id"))
        .Select(col => col.ColumnName);

header.AddRange(headers);

