LINQ Query returning all original objects I'm just refactoring some code I've come across whilst fixing a bug, but just wanted a quick sanity check to make sure I'm not missing any reason for keeping what I'm going to remove.

The current code:

uxButton.Enabled = ((from a in actions select a).Distinct().Count() == 1);


There really doesn't seem to be a need for the LINQ query, so:

uxButton.Enabled = actions.Distinct().Count() == 1;


Should do the trick, right?
As the other answers stated your given code does do the same thing, but could give performance issues for big collections since the entire collection is traversed when calling Count().

The following code should solve this:

var distinct = actions.Distinct();
uxButton.Enabled = distinct.Take( 2 ).Count() == 1;


Perhaps you could turn this into an extension method:

uxButton.Enabled = actions.Distinct().CountOf( 1 );




It seemed useful, so I added it to my library, along with a unit test.

/// &lt;summary&gt;
///   Returns whether the sequence contains a certain amount of elements.
/// &lt;/summary&gt;
/// &lt;typeparam name = "T"&gt;The type of the elements of the input sequence.&lt;/typeparam&gt;
/// &lt;param name = "source"&gt;The source for this extension method.&lt;/param&gt;
/// &lt;param name = "count"&gt;The amount of elements the sequence should contain.&lt;/param&gt;
/// &lt;returns&gt;
///   True when the sequence contains the specified amount of elements, false otherwise.
////&lt;/returns&gt;
public static bool CountOf&lt;T&gt;( this IEnumerable&lt;T&gt; source, int count )
{
    Contract.Requires( source != null );
    Contract.Requires( count &gt;= 0 );

    return source.Take( count + 1 ).Count() == count;
}