After digging on this particular question i have found if we use a custom query in our controller we can get all product including parent products and child-products all in one.
Inject this to controller
\Magento\Framework\App\ResourceConnection $resourceConnection,
Create object in __construct
$this->_resources = $resourceConnection;
In your Function use
$connection= $this->_resources->getConnection();
$select = $connection->select()
->from(['o' => $this->_resources->getTableName('catalog_category_product')]);
$select->where('category_id = ?', 'add your category id' here);
$result = $connection->fetchAll($select);
We get Category products ids in $result and now for using Product_id separately use foreach and save
$proIds = array();
foreach ($result as $product)
{
$proIds[] = $product['product_id'];
}
Then inject \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory class and use it with $this->productCollectionFactory to filter your products from all products.
$collect = $this->productCollectionFactory->create()
->addFieldToFilter('entity_id',array('in',$proIds));
Use foreach to deal with products separately
foreach ($collect as $Product)
{
***"Do what ever you want with product"***
}
If that helps hit like thanks