Home > Back-end >  Linq for nested object in cosmos db json
Linq for nested object in cosmos db json

Time:11-20

Hi there I cant figure out euivalent of this cosmos query in linq

Cosmos Query:

select c_option
from c
join c_size in c.size
join c_variant in c_size.variant
join c_option in c_variant.option
where c.type = 'product'
and c_option.optionID = '869' 

And my linq but it returns null all the time ehh.

query.SelectMany(product => product.Size
.SelectMany(size => size.Variant
.SelectMany(variant => variant.Option
.Where(option => option.OptionId
.Equals(optionId, StringComparison.OrdinalIgnoreCase))))));

Option is nested child object in product . Nesting looks like that product->size->variant->option

Product Model is a class with properties of type int/string and also collection of size

public class Product {
 int id {get;set;}
 IList<Size> Size {get;set;}
}
public class Size {
 int id {get;set;}
 IList<Variant> Variant {get;set;}
}
public class Variant {
 int id {get;set;}
 IList<Option> Options {get;set;}
}
public class Option {
 int id {get;set;}
 IList<OptionAvailability> OptionsAvas {get;set;}
}

Result: I need to get an option by option Id

CodePudding user response:

It tured out I got a spelling issue in the json property, optionID instead of optionId. Besides that everything works now.

Cheers.

  • Related