【问题标题】:c# - find a string from a nested listc# - 从嵌套列表中查找字符串
【发布时间】:2022-03-03 13:36:34
【问题描述】:

我有以下嵌套结构。

List<SomeType> 有另一个List<AnotherType> 列表,内部有一个array of strings users

public class SomeType
{
    public string EmployeeNo { get; set; }

    public List<AnotherType> AnotherType { get; set; }
}

public class AnotherType
{
    public string UserName { get; set; }

    public string[] Users { get; set; }
}

假设我想在字符串Users 的数组中搜索一个值。我该怎么做。

【问题讨论】:

  • 必须先显示 SomeType 和 AnotherType 才能回答
  • 向我们展示您已经尝试过的代码并解释它是如何失败的,以便我们建议如何修复代码。
  • @pm100 更新了代码
  • “在字符串用户数组中搜索一个值”属于哪个列表或对象?向我们展示样品。

标签: c# linq


【解决方案1】:

您是否正在尝试做这样的事情?

var query = SomeTypeList
    .Where(sometype => sometype.AnotherType.Any(
        anothertype => anothertype.Users.Any(
             user => user.Contains("user to search for"))))

看起来像重复:stackoverflow: C# LINQ Filter deep nested list

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-15
    • 2016-02-08
    • 2019-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-07
    • 1970-01-01
    相关资源
    最近更新 更多