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.
Why?
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
Solution
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.