嵌套列表如何使用linq提取对象

ylamdve6  于 2023-03-05  发布在  其他
关注(0)|答案(2)|浏览(125)

我有下面的嵌套列表,我想提取具有特定uiparameterName的uiparameter对象,但我不知道如何编写linq表达式。

public class root
{
  public List<module> module{get; set;}
}

public class module
{
 public int moduleID {get; set;}
 public string moduleName {get; set;}
 public List<parameter> parameters {get;set;}
}

public class parameter
{
 public int parameterID {get; set;}
 public string parameterName {get; set;}
 public List<uiparameter> uiparameters{get;set;}

}
public class uiparameter
{
 public int uiparameterID {get; set;}
 public string uiparameterName {get; set;}
 public string uiparameterdesc {get; set;}

}
py49o6xq

py49o6xq1#

看起来,您正在寻找SelectManyflatten 嵌套列表:

root myRoot = ...

  var result = myRoot
    .module
    .SelectMany(m => m.parameters)
    .SelectMany(p => p.uiparameters)
    .FirstOrDefault(ui => ui.uiparameterName == "PutRequiredNameHere");
cnh2zyt3

cnh2zyt32#

应该使用SelectMany将其展平,如下所示:

var uiParams = myRoot.module.SelectMany(x => x.parameters)
                            .SelectMany(y => y.uiparameters)
                            .Where(z => z.uiparameterdesc == "I'm a ui param");

这将返回一个IEnumerable,其中包含与您搜索的描述匹配的所有对象。
这是在一个控制台应用程序中工作:

static void Main(string[] args)
    {
        var myRoot = new root
        {
            module = new List<module>
            {
                new module
                {
                    moduleID = 1,
                    moduleName = "moduleA",
                    parameters = new List<parameter>
                    {
                        new parameter
                        {
                            parameterID = 1,
                            parameterName = "paramA",
                            uiparameters = new List<uiparameter>
                            {
                                new uiparameter { uiparameterID = 1, uiparameterName = "uiParamA", uiparameterdesc = "I'm a ui param" },
                                new uiparameter { uiparameterID = 2, uiparameterName = "uiParamB", uiparameterdesc = "I'm a ui param too" }
                            }
                        },
                        new parameter
                        {
                            parameterID = 2,
                            parameterName = "paramB",
                            uiparameters = new List<uiparameter>
                            {
                                new uiparameter { uiparameterID = 3, uiparameterName = "uiParamC", uiparameterdesc = "I'm a ui param" },
                                new uiparameter { uiparameterID = 4, uiparameterName = "uiParamD", uiparameterdesc = "I'm a ui param too" }
                            }
                        }
                    }
                },
                new module
                {
                    moduleID = 2,
                    moduleName = "moduleB",
                    parameters = new List<parameter>
                    {
                        new parameter
                        {
                            parameterID = 1,
                            parameterName = "paramC",
                            uiparameters = new List<uiparameter>
                            {
                                new uiparameter { uiparameterID = 5, uiparameterName = "uiParamE", uiparameterdesc = "I'm a ui param" },
                                new uiparameter { uiparameterID = 6, uiparameterName = "uiParamF", uiparameterdesc = "I'm a ui param too" }
                            }
                        },
                        new parameter
                        {
                            parameterID = 2,
                            parameterName = "paramD",
                            uiparameters = new List<uiparameter>
                            {
                                new uiparameter { uiparameterID = 7, uiparameterName = "uiParamG", uiparameterdesc = "I'm a ui param" },
                                new uiparameter { uiparameterID = 8, uiparameterName = "uiParamH", uiparameterdesc = "I'm a ui param too" }
                            }
                        }
                    }
                }
            }
        };

        var uiParams = myRoot.module.SelectMany(x => x.parameters)
                                    .SelectMany(y => y.uiparameters)
                                    .Where(z => z.uiparameterdesc == "I'm a ui param");

        Console.WriteLine("UI Parameters:");
        foreach (var uip in uiParams)
        {
            Console.WriteLine("Param Id: " + uip.uiparameterID + ", Param Name: " + uip.uiparameterName);
        }

        Console.ReadLine();
    }

结果:

相关问题