如何在 C# 中访问对象的嵌套列表

本文关键字:对象 嵌套 列表 访问 | 更新日期: 2023-09-27 18:34:39

假设我有一个List<B>,它是A的属性。

现在我正在尝试访问List<B> from List<A>.

Class A
{
    int ID;
    string Name;
}
Class B
{
    List<A> a;
    int ID;
    string Name;
}
Class ViewModel
{
    List<B> b;
}
Class Main
{
    ViewModel _model = new ViewModel();
    //I want to set _model.b.a to List<A> variable
}

有什么方法可以使用它吗?

如何在 C# 中访问对象的嵌套列表

_model.b是一个

列表,所以如果你想为列表中的每个项目设置a的值,你可以这样做:

Class Main
{
    var yourValues = new List<A>();
    ViewModel _model = new ViewModel();
    foreach (var item in _model.b)
    {
       item.a = yourValues;
    }
}

您也可以使用 ForEach 执行循环:

model.b.ForEach(i => i.a = yourValues);

你可以这样做的一种方法是这样的:

https://dotnetfiddle.net/6knni1

using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
    public static void Main()
    {
        ViewModel _model = new ViewModel();
        var AllOfMyAs = _model.Bs.SelectMany(sm => sm.As).ToList(); // Here's the "main event"
        Console.WriteLine(AllOfMyAs.Count());
        Console.WriteLine("");
        foreach (A a in AllOfMyAs)
        {
            Console.WriteLine(a.Name);
        }
    }
}
public class A
{
    public int ID {get; set; }
    public string Name { get; set; }
}
public class B
{
    public List<A> As { get; set; }
    public int ID { get; set; }
    public string Name { get; set; }
    public B()
    {
        As = new List<A>();
    }
}
public class ViewModel
{
    public List<B> Bs { get; set; }
    public ViewModel()
    {
        Bs = new List<B>()
        {
            new B()
            {
                As = new List<A>()
                {
                    new A()
                    {
                        Name = "test1"
                    },
                    new A()
                    {
                        Name = "test2"
                    }
                }               
            },
            new B()
            {
                As = new List<A>()
                {
                    new A()
                    {
                        Name = "another B"
                    }
                }
            }
        };
    }
}

根据评论 https://dotnetfiddle.net/l8g2Du:

using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
    public static void Main()
    {
        ViewModel _model = new ViewModel();
        _model.Bs.ForEach(fe => fe.As = PlaceHolderAsData());
    }
    public static List<A> PlaceHolderAsData()
    {
        return new List<A>()
        {
            new A()
            {
                Name = "blah blah"
            },
            new A()
            {
                Name = "Bob Loblaw"
            }
        };
    }
}
public class A
{
    public int ID {get; set; }
    public string Name { get; set; }
}
public class B
{
    public List<A> As { get; set; }
    public int ID { get; set; }
    public string Name { get; set; }
    public B()
    {
        As = new List<A>();
    }
}
public class ViewModel
{
    public List<B> Bs { get; set; }
    public ViewModel()
    {
        Bs = new List<B>()
        {
            new B()
            {
                As = new List<A>()
            },
            new B()
            {
                As = new List<A>()
            }
        };
    }
}

正如我上面写的评论所暗示的那样,你的前提有些复杂,所以这个问题很难回答,因为我不确定你真正的意图是什么。

如果它确实像您所说正在使用列表...

你要求做的是这样的:

// create the list of B objects...
_model.b = new List<B>();
// add one B object to the list...
_model.b.Add(new B());
// set the first (and only) B object in the list's a property to a new list of A objects
_model.b[0].a = new List<A>();
// optionally add an A object to the List<A> property
_model.b[0].a.Add(new A());

但我认为你想要的是完全不同的东西:

public Class A
{
    public int ID;
    public string Name;
}
public Class B : A
{
    public List<A> a = new List<A>();
}
Class ViewModel
{
    public List<B> b = new List<B>();
}
Class Main
{
    ViewModel _model = new ViewModel();
    //I want to set _model.b.a to List<A> variable
    ==> not sure what you want here ==> there is no _model.b.a since b is a list of A objects not an instance of B itself.
}