1

I have a list of strings like this

"Users": [
    "usrName|Fullname|False|0|False|False",
    "usrName|Fullname|False|0|False|False",
    "usrName|Fullname|False|0|False|False",
    "usrName|Fullname|False|0|False|False",
    "usrName|Fullname|False|0|False|False",
   
]

In my episerver/optimizely code I want to match items. I have written this line of code

searchResult.Filter(x => x.Users.MatchContained(k=> k.Split('|')[3], "0"));

I am trying to get all Users where after splitiing on 'pipe' I get 0 at index 3. I am not getting the result, infact I get an exception.

What am I doing wrong here?

mohsinali1317
  • 4,255
  • 9
  • 46
  • 85

1 Answers1

1

Since you left out A LOT of details these are my assumptions

  • Users is an IList<string> Users
  • You are trying to get all Users within that list/array where index 3 equals 0

What we don't know is, among others

  • Is there more than one page instance that have the Users instance filled?

Anyway, this cannot be solved using any Find API with the current system design. Instead you need to rely on linq to parse the result, but then the Find implementation may not be necessary.

var searchClient = SearchClient.Instance;
var search = searchClient.Search<BlogPage>();
var result = search.GetContentResult();

var usersResult = result
    .SelectMany(x => x.Users)
    .Where(x => x.Split('|')[3].Equals("0"));

This would create and return an object similar to this, since I'm using SelectMany the array would contain all users throughout the systems where there are matched pages from the search result.

enter image description here

If you for whatever reason would like to keep or see some page properties there are alternative approaches where you construct a new object within a select and remove any object where there where not matched users

var searchClient = SearchClient.Instance;
var search = searchClient.Search<BlogPage>();
var result = search.GetContentResult();

var usersResult = result
    .Select(p => new
    {
        PageName = p.Name,
        ContentLink = p.ContentLink.ToString(),
        Users = p.Users.Where(x => x.Split('|')[3].Equals("0"))
    })
    .Where(x => x.Users.Any());

enter image description here

If you like to keep using Find for this kind of implementations you must store the data in a better way than strings with delimiters.

Eric Herlitz
  • 25,354
  • 27
  • 113
  • 157