Fetch all variants connected to a specific inventory

How can I fetch all varaiants in a specific inventory?

Litium version: [7.2]

The InventoryItemService has a method GetByInventory, https://docs.litium.com/api-reference/api?product=Litium&version=7.2&xref=Litium.Products.InventoryItemService

var inventoryItems = _inventoryService.GetByInventory(inventorySystemId);

This gets you a list of InventoryItem which has a property VariantSystemId,

1 Like

Thank you Nils!
Is this the best/most efficient way to fetch them? I hoped that I could fetch a list of variants “directly”.
This will work but I need to iterate over all VariantSystemIds and call variantService.Get() on each id to fetch the variant-object. It feels pretty expensive :smile:

VariantService has an overload of Get that takes a list of GUIDs so you can get all in one call, https://docs.litium.com/api-reference/api?product=Litium&version=7.2&xref=Litium.Products.VariantService

So you could do something like (dry-coded):

var inventoryItems = _inventoryService.GetByInventory(inventorySystemId);
var variantSystemIds = inventoryItems.Select(x => x.VariantSystemId);
var variants = _variantService.Get(variantSystemIds);
1 Like

You can also try this one:

var variants = _variantService.Get(_inventoryItemService.GetByInventory(inventorySystemId).Select(x => x.VariantSystemId));
1 Like

Thank you for your help @steve.redstorm and @NilsN .
I didn’t know about that overload @NilsN. Thanks! :slight_smile:

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.