形成具有替代行或某些属性的列表对象

form an list object with alternative rows or some of properties

我有一个平面物体,它看起来像下图

public class Test 
{
   public string Name {get; set;}
   public string Number {get; set;}
   public string Description {get; set;}
   public string Name1 {get; set;}
   public string Name2 {get; set;}
}

我的数据如下所示

List<Test> tests = new List<Test>
{
    new Test{ Name = "nameA", Number=8, Description="description", Name1 = "test 1", Name2="test name1" },
    new Test{ Name = "nameB", Number=3, Description="description2", Name1 = "test 2", Name2="test name2" },
    new Test{ Name = "nameC", Number=4, Description="description3", Name1 = "test 3", Name2="test name3" },
};

我正在尝试形成如下所示的对象列表

我正在使用以下逻辑形成列表对象,但未能将所有对象添加到其中,

List<dynamic> testList = new();

int rowIndex = 1;
foreach (var testDt in tests)
{
    dynamic testData = new ExpandoObject();

    if (rowIndex % 2 != 0)
    {
        testData.Name = testDt.Name;
        testData.Description = testDt.Description;
        testData.Number = testDt.Number;
    }
    else
    {           
        testData.Name1 = testDt.Name1;
        testData.Name2 = testDt.Name2;
    }
    testList.Add(testData);
    rowIndex++;
}

但是如果列表中有更多项目,此逻辑不会将所有项目添加到列表中,任何人都可以告诉我上面代码哪里错了。

非常感谢!!!

给你:

List<Test> tests = new List<Test>
{
    new Test{ Name = "nameA", Number = 8, Description = "description", Name1 = "test 1", Name2 = "test name1" },
    new Test{ Name = "nameB", Number = 3, Description = "description2", Name1 = "test 2", Name2 = "test name2" },
    new Test{ Name = "nameC", Number = 4, Description = "description3", Name1 = "test 3", Name2 = "test name3" },
};

var results  = 
    from t in tests
    from r in new []
    {
        new { t.Name, Number = t.Number.ToString(), t.Description, Name1 = "", Name2 = "", },
        new { Name = "", Number = "", Description = "", Name1 = t.Name1, t.Name2, },
    }
    select r;

这给了我:

要使用常规 foreach 循环,这会起作用:

public class Output
{
    public string Name { get; set; }
    public string Number { get; set; }
    public string Description { get; set; }
    public string Name1 { get; set; }
    public string Name2 { get; set; }
}

var results = new List<Output>();
foreach (var t in tests)
{
    results.Add(new Output() { Name = t.Name, Number = t.Number.ToString(), Description = t.Description, Name1 = "", Name2 = "", });
    results.Add(new Output() { Name = "", Number = "", Description = "", Name1 = t.Name1, Name2 = t.Name2, });
}

我创建了一个新的 class 而不是使用 dynamic,因为 dynamic 通常产生的问题多于它解决的问题,IMO。我尽可能避免它。

最后,等效于使用 LINQ 的方法语法:

results =
    tests
        .SelectMany(t => new []
        {
            new Output() { Name = t.Name, Number = t.Number.ToString(), Description = t.Description, Name1 = "", Name2 = "", },
            new Output() { Name = "", Number = "", Description = "", Name1 = t.Name1, Name2 = t.Name2, },
        })
        .ToList();

问题是您每次迭代只向列表添加 1 项,但是,您需要添加 2 项而不是才能获得预期结果。

您可以简单地按如下方式进行。

foreach (var testDt in tests)
{
    dynamic testData1 = new ExpandoObject();
    testData1.Name = testDt.Name;
    testData1.Description = testDt.Description;
    testData1.Number = testDt.Number;
    testList.Add(testData1);

    dynamic testData2 = new ExpandoObject();
    testData2.Name1 = testDt.Name1;
    testData2.Name2 = testDt.Name2;
    testList.Add(testData2);
}