Check for Duplicate – A LINQ story C#

Came across this the other day. Replaced type names and variables to be generic and added some line breaks to fit here. Putting aside any feelings about the function interface itself this method functions correctly and does its job. Some would argue that rather than using a result variable that only gets returned we could just return true/false from each location directly. This would also be ok.

public bool CheckForDuplicate(string name, User user, int id = -1)
{    
    var item = user.Items.SingleOrDefault(x => x.Name.Equals(name, 
                   StringComparison.InvariantCultureIgnoreCase));

    bool result;
    if (item != null) {
        //means a record was found with the same Item	
        if (item.Id == Id) {
            //this means we're updating the same record, so it's OK
            result = false;
        } else {
            // 2 different records with same item name
            result = true;
        }
    } else {
        //duplicate not found
        result = false;
    }

    return result;
}

However, this method can easily be simplified to a single line of LINQ.

public bool CheckForDuplicate(string name, User user, int id = -1)
{
    return user.Items.Any(x => x.Name.Equals(name, 
               StringComparison.InvariantCultureIgnoreCase) 
               && x.Id != id);
}

Not relevant to this code itself, but User was actually a NHibernate entity, and Items was a lazy load mapping. Using .Any only requires a yes/no type of return rather than loading the Item entity itself. Though the difference minimal it is preferable.

Bleeps Run Gameplay Video