Ralph Shillington
Ralph Shillington

Reputation: 21098

Can this lambda be re-written as a query expression

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"

Upvotes: 2

Views: 180

Answers (2)

Amy B
Amy B

Reputation: 110071

select t).Count() 

or going for terse-ness in the other direction:

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

Upvotes: 3

David
David

Reputation: 8640

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()

Upvotes: 0

Related Questions