easiest way is to use XrmServiceToolkit:
http://xrmservicetoolkit.codeplex.com/
then use SOPA with Fetch:
http://xrmservicetoolkit.codeplex.com/wikipage?title=Soap%20Functions
And here you have the fetchXml example:
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false" aggregate="true">
<entity name="account">
<attribute name="accountid" aggregate="count" alias="aliasaccountid"/>
<filter type="and">
<condition attribute="ownerid" operator="eq-userid" />
<condition attribute="statecode" operator="in">
<value>1</value>
</condition>
</filter>
</entity>
</fetch>
aggregate="true" in header and attribute with aggregate="count" and alias!
https://msdn.microsoft.com/en-us/library/gg309565.aspx