Thoughts and Rants

My random thoughts and rants on things

LINQ is your friend

I have been writing code for a fair amount of time and until very recently I was on the fence about LINQ.  Recently I picked up a project that relied heavily on the use of LINQ to retrieve data from IEnumerable types.  After a few non-starts for me I quickly realized the power of LINQ.  For example I used to do this:

List<MyObj> myList = new List<MyObj>();
MyObj myObj = null;

foreach( var _obj in myList )
{
    if( string.Compare(_obj.ItemName, "SomeName", true) == 0 )
    {
        MyObj = _obj;
        break;
    }
}

With LINQ, I can do this:

List<MyObj> myList = new List<MyObj>();
MyObj myObj = null;

myObj = myList.Where( o => o.ItemName == "SomeName" ).FirstOrDefault();

So LINQ has finally won me over.

Loading