如何从指定数量的模型/类创建列表列表

本文关键字:列表 模型 创建 | 更新日期: 2023-09-27 18:36:43

好吧,这个问题/疑问的标题我认为这是不言自明的,但这里有这个想法(使用简单的术语):我有文件(在一个项目中),其中包含一个类(每个类),该类具有对象和方法(来自模型),其中一个方法返回一个列表。我想创建另一个类来生成一个新列表,其中包含上述所有列表。如果这主要在 C# 中是可能的,我将感谢您对如何创建它的观点。提前感谢您的提示、帮助和善意!!

我希望你能理解我,因为我在描述问题方面很糟糕。 :D

如何从指定数量的模型/类创建列表列表

你要找的是SelectMany:

#region Terrible Object
var hasAllTheItems =
        new[]
        {
                new[]
                {
                        new
                        {
                                Name = "Test"
                        }
                },
                new[]
                {
                        new
                        {
                                Name = "Test2"
                        },
                        new
                        {
                                Name = "Test3"
                        }
                }
        };
#endregion Terrible Object
var a = hasAllTheItems.Select(x => x.Select(y => y.Name));
var b = hasAllTheItems.SelectMany(x => x.Select(y => y.Name));
var c = hasAllTheItems.Select(x => x.SelectMany(y => y.Name));
var d = hasAllTheItems.SelectMany(x => x.SelectMany(y => y.Name));
Assert.AreEqual(2, a.Count());
Assert.AreEqual(3, b.Count());
Assert.AreEqual(2, c.Count());
Assert.AreEqual(14, d.Count());

A: {{Test}, {Test2, Test3}}

B: {Test, Test2, Test3}

C: {{T, e, s, t}, {T, e, s, t, 2, T, e, s, t, 3}}

D: {T, e, s, t, T, e, s, t, 2, T, e, s, t, 3}