Try our conversational search powered by Generative AI!

Problem with deserialization of lists with Find GetResult

Vote:
 

Hello,

I'm trying to deserialize an index of our Product object and it crashes on lists. Here is my code:

                var query = _client.Search<Product>().For("product1");
                var result = query.Select(x => new { m = x.MarketFilter }).GetResult();

The thrown exception reads "'To deserialize objects the $type property must be set." There is no $type property in the Index, should I force it somehow?

    "MarketFilter": [
        "DEFAULT",
        "FRMARKET",
        ...
    ], 

It's not possible for any list but MarketFilter is a native property so I thought it should work at least with that.

Thanks.

Epi CMS: 11.21.2
Epi commerce: 13.32.1
EPiServer.Find: 13.4.8.0

#318950
Mar 14, 2024 11:34
Vote:
 

Hi Petr,

What happens if you had

    var result = query.Select(x => new { m = x.MarketFilter.ToList() }).GetResult();
#318953
Mar 14, 2024 12:17
Vote:
 

Hi Paul,

although the type is changed from ItemCollection<string> to List<string>, the Exception stays the same.

#318954
Edited, Mar 14, 2024 12:44
Paul McGann (Netcel) - Mar 14, 2024 17:13
Yeah I think that is because the items under the hood is an IList. Thought trying the ToList might have got around it.
Vote:
 

You probably want to do this instead 

var query = _client.Search<Product>().For("product1");
                var result = query.Select(x => new { m = x.Markets() }).GetResult();

Markets is an extension that is the opposite of MarketFilter (which means the markets the product is excluded in) 

#318963
Mar 14, 2024 15:55
Vote:
 

While not specific to the markets example above.   If you want to use projections from TypeSearch with a collection property, then you should ensure the property is declared as IEnumerable<T>.  I've had allsorts of issues with IList<T> and List<T> where they did not serialize back or caused errors.  It only started working for me when I declared the property being indexed as IEnumerable

e.g.

public class MyPage
{
  [Searchable]
  public IEnumerable<ChildModel> Children => GetChildren();

  private List<ChildModel> GetChildren()
  {
     // Logic here to return a List<ChildModel>
  }
}
#319134
Mar 18, 2024 14:46
Vote:
 

Thinking out loud but that would be very strange to be honest. Json can't be serialized to a deferred iterator, it has to serialize to a concrete class (in this case, a List, or an array). I have no idea why it has problem deserializing to a List however ...

#319136
Mar 18, 2024 14:55
Vote:
 

While not specific to the markets example above.   If you want to use projections from TypeSearch with a collection property, then you should ensure the property is declared as IEnumerable<T>.  I've had allsorts of issues with IList<T> and List<T> where they did not serialize back or caused errors.  It only started working for me when I declared the property being indexed as IEnumerable

That was it! When I changed IList to IEnumerable in my Epi model, it started working. Thanks!

public class Product : ProductContent {

        [Display(
           Name = "Product type list",
           GroupName = GroupNames.MetaData,
           Order = 15)]
        [BackingType(typeof(PropertyStringList))]
        public virtual IEnumerable<String> ProductTypeList { get; set; }

}
#319253
Edited, Mar 20, 2024 14:09
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.