tags:

views:

69

answers:

2

I often want just the total count(or other aggregate) of a selection, typically when cruising through some data with LinqPad for example this query that returns the number of public enums in the core lib.

"".GetType().Assembly.GetTypes().Where(t => t.IsPublic && t.IsEnum).Count()

I know that I could do something like this:

(from t in "".GetType().Assembly.GetTypes() 
where t.IsEnum  && t.IsPublic select t.Name).Count()

but maybe because of too many years in T-SQL, I find the "select t.Name" a bit off-putting.

Is there another way to get this Count without stating the "select t.Name"

A: 

Off the top of my head, you would need to group by t

         from t in "".GetType().Assembly.GetTypes()
         where t.IsEnum && t.IsPublic
         group t by t into x
            select x.Count()
Dve
No I think that will result in 195 instances of 1. (there appears to be 195 public enums in mscorlib 2.0.0.0)However group t by t.IsEnum into x does the trick. But having said that I'm not sure if the query expression is more palatable than the lamda expression approach. Thanks for the hint.
Ralph Shillington
+3  A: 
select t).Count() 

or going for terse-ness in the other direction:

"".GetType().Assembly.GetTypes().Count(t => t.IsPublic && t.IsEnum)
David B
Or even just `select 1`
AakashM