如何在情节中的字符串列表中搜索substring

发布于 2025-02-04 07:38:35 字数 533 浏览 2 评论 0 原文

有这样的字符串列表

"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",
   
]

我想匹配项目中的情节/优化代码中 。我已经写了这条代码行,

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

我试图让所有用户在“管道”上分开后,我在索引3上得到0。我没有得到结果,我没有得到一个例外。

我在这里做错了什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

远山浅 2025-02-11 07:38:36

由于您忽略了很多细节,因此我的假设

  • 用户 ilist< string>用户
  • 您正在尝试将索引3等于0等于0的列表/数组中的所有用户,

我们不知道的是,还有

  • 多个页面实例以外的实例填充了吗?

无论如何,这无法使用当前系统设计的任何查找API解决。取而代之的是,您需要依靠LINQ来解析结果,但是可能不需要发现实现。

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"));

这将创建并返回类似于此的对象,因为我使用的是SelectMany,该数组将在整个系统中包含来自搜索结果中匹配页面的所有用户。

如果您出于任何原因要保留或查看某些页面属性,则有其他方法在选择中构造一个新对象,并删除在不匹配的用户

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());

如果您想继续使用此类实现,则必须比使用定界符的字符串以更好的方式存储数据。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文