score:11

Accepted answer

Use var instead of object. The select new creates an anonymous type:

foreach (var oParam in objList)
{
    Console.WriteLine(oParam.UserID);
}

score:4

You could also use a small extension method (credits):

public static void ForEach<T>(this IEnumerable<T> @this, Action<T> action)
{
    foreach (var item in @this)
        action(item);
}

Then you can do

objList.Foreach(oParam =>
{
    ... // your code here, e.g. Console.WriteLine(oParam.UserID)
}

It's really good in method-syntax linq.


Related Articles