When you have a query with a Common Table Expression the query.AsCount().Get() function will return "Invalid object name 'cte0'." When I print the SQL of query.AsCount() the WITH block is missing.