C# LINQ standards for Finding and Getting

A bit of a semantics thing here: recently I’ve been creating a lot of extension methods over using LINQ. I used to call my “search” operation GetByID, e.g.:

public Person GetByID(this List persons, personID)
{
    return persons.SingleOrDefault(p => p.PersonID == personID)
}

However, that then means I need to check for null after calling persons.GetByID(blah), which seems a bit odd for an explicit Get operation, as in “I’m sure this data is in here, if not, than an Exception would be permissible”.

So, now for extension method LINQ queries that use .SingleOrDefault I use FindByID to remind me to check for null afterwards, and my extension method GetByID now uses the LINQ .Single operator / thingy.

Very quick post, but there ya go :)

About mike

A nice guy, web developer using ASP.Net, martial arts fan and practitioner (when not crippled), film buff, husband and father :-)