Count total number of records – how to??

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)

Count total number of records – what is the issue

How simple can that be to count total number of records – just use linq query using EntitySet object like the one below

var contactCounter = new ServiceContext(_service).ContactSet.Select(item => item).Count();

and that’s it. But no – you will get an exception:

Invalid ‘count’ condition. An entity member is invoking an invalid property or method.


If we use var as variable type (rather than strongly typed) then Select linq statement will use IQueryable as a default output type. Simple trick is to declare variable type and use CreateQuery statement when using OrganizationServiceContext


 IEnumerable<Entity> contacts = new ServiceContext(_service).CreateQuery(Contact.EntityLogicalName).Select(item => item);
            var contactCountere = contacts.Count();

P.S. If you found this post useful please rate it.

Have an own opinion?