C#对象列表可以声明,返回要插入主列表的项的迭代。

huangapple go评论72阅读模式
英文:

C# object list that can be declared, returning an iteration of items to be inserted into the main list

问题

I understand your request, and here's the translated portion of your text:

C#问题。

我需要填充我的主对象属性,这是一个自定义对象列表,但在某些时候,我需要向这个列表中添加一个具有特定声明的复制,但当列表中的特定ID的项目仅发生索引更改时,我不想为每个项目添加一行。

如何使用循环将项目添加到构造函数声明中的列表。

元代码示例:

private static readonly MainObject myObject = new()
{
  Type = "OP",
  Group = "FD",
  Items = new List<SubObject>
  {
    new SubObject("Tag.Count", 0),
    new SubObject("Tag.Min", 0),
    new SubObject("Tag.Max", 100),

    // 子列表从这里开始,从1到10
    new SubObject("Tag.Pos.1", 1),
    new SubObject("Tag.Pos.2", 4),
    new SubObject("Tag.Pos.3", 9),
    ...
    new SubObject("Tag.Pos.10", 100),
    // 子列表在这里以10个项目结束

  }
}

因此,我的SubObject列表包含了可以解决为单行迭代的内容,但如何在此声明中执行它,...类似于,...其中i是从1到10的循环项目:

            ... new SubObject(`Tag.Pos.$i`, $i^2)

寻找一个解决方案,最终返回一个子列表,可以在声明对象时添加和包含在主列表中,并被构造函数识别和接受:

类似于JavaScript中可以返回类似以下内容的内容:

...[new SubObject(`Tag.Pos.$i`, $i^2)]

Please note that the translation provided may not include code formatting.

英文:

C# question.

I need to fill my main object property, that is a list of a custom object, but at some point, I need to add to this list a replication with a specific declaration, but I don't want to have to add a line per each item, when there's only and index change on the specific ID of the item in the list.

How to do that with a loop that adds items to a list on the constructor declaration (or so).

Metacode sample:

private static readonly MainObject myObject = new()
{
  Type = &quot;OP&quot;,
  Group = &quot;FD&quot;,
  Items = new List&lt;SubObject&gt;
  {
    new SubObject(&quot;Tag.Count&quot;, 0),
    new SubObject(&quot;Tag.Min&quot;, 0),
    new SubObject(&quot;Tag.Max&quot;, 100),

    // Sub list starts here, from 1 to 10
    new SubObject(&quot;Tag.Pos.1&quot;, 1),
    new SubObject(&quot;Tag.Pos.2&quot;, 4),
    new SubObject(&quot;Tag.Pos.3&quot;, 9),
    ...
    new SubObject(&quot;Tag.Pos.10&quot;, 100),
    // Sub list ends here with 10 items

  }
}

So, my SubObject list contains what could be solved as a iteration of a single line, but how to do it in this declaration, ... something like, ... were i is a loop item from 1 to 10:

            ... new SubObject(`Tag.Pos.$i`, $i^2)



Looking for a solution that in the end, returns a sub list that can be added and included in the main list when declaring the object, recognized and accepted by the constructor:

Something that, just line in JavaScript can return something similar to:

...[new SubObject(`Tag.Pos.$i`, $i^2)]

答案1

得分: 0

你可以尝试使用for循环。

private static readonly MainObject myObject = new MainObject
{
    Type = "OP",
    Group = "FD",
    Items = GenerateSubList()
};

private static List<SubObject> GenerateSubList()
{
    List<SubObject> subList = new List<SubObject>
    {
        new SubObject("Tag.Count", 0),
        new SubObject("Tag.Min", 0),
        new SubObject("Tag.Max", 100)
    };

    for (int i = 1; i <= 10; i++)
    {
        subList.Add(new SubObject($"Tag.Pos.{i}", i * i));
    }

    return subList;
}
英文:

you can try with for loop

private static readonly MainObject myObject = new MainObject
{
    Type = &quot;OP&quot;,
    Group = &quot;FD&quot;,
    Items = GenerateSubList()
};

private static List&lt;SubObject&gt; GenerateSubList()
{
    List&lt;SubObject&gt; subList = new List&lt;SubObject&gt;
    {
        new SubObject(&quot;Tag.Count&quot;, 0),
        new SubObject(&quot;Tag.Min&quot;, 0),
        new SubObject(&quot;Tag.Max&quot;, 100)
    };

    for (int i = 1; i &lt;= 10; i++)
    {
        subList.Add(new SubObject($&quot;Tag.Pos.{i}&quot;, i * i));
    }

    return subList;
}

答案2

得分: 0

你不能在声明块中动态添加项目,但可以在构造后使用 Linq 添加项目:

  Items = new List<SubObject>
  {
    new SubObject("Tag.Count", 0),
    new SubObject("Tag.Min", 0),
    new SubObject("Tag.Max", 100),
  }

  myObject.Items.AddRange(Enumerable.Range(1, 10).Select(
    i => new SubObject($"Tag.Pos.{i}", i * i)
  ));

或者将 "Items" 生成信息移到一个返回可变数量项目的函数中:

private static List<SubObject> GetItems(int numItems)
{
    List<SubObject> items = new List<SubObject>
    {
        new SubObject("Tag.Count", 0),
        new SubObject("Tag.Min", 0),
        new SubObject("Tag.Max", numItems * numItems)
    };

    items.AddRange(Enumerable.Range(1, numItems).Select(
      i => new SubObject($"Tag.Pos.{i}", i * i)
    ));

    return items;
}

然后在构造函数中调用:

private static readonly MainObject myObject = new()
{
  Type = "OP",
  Group = "FD",
  Items = GetItems(10)
};
英文:

You can't dynamically add items in the declaration block, but you could add items after construction using Linq:

  Items = new List&lt;SubObject&gt;
  {
    new SubObject(&quot;Tag.Count&quot;, 0),
    new SubObject(&quot;Tag.Min&quot;, 0),
    new SubObject(&quot;Tag.Max&quot;, 100),

  }

  myObject.Items.AddRange(Enumerable.Range(1,10).Select(
    i =&gt; new SubObject($&quot;Tag.Pos.{i}&quot;, i*i)
  ));

or move the "Items" generation info a function that returns a varaible number of items in a list:

private static List&lt;SubObject&gt; GetItems(int numItems)
{
    List&lt;SubObject&gt; items = new List&lt;SubObject&gt;
    {
        new SubObject(&quot;Tag.Count&quot;, 0),
        new SubObject(&quot;Tag.Min&quot;, 0),
        new SubObject(&quot;Tag.Max&quot;, numItems*numItems)
    };

    items.AddRange(Enumerable.Range(1,numItems).Select(
      i =&gt; new SubObject($&quot;Tag.Pos.{i}&quot;, i*i)
    ));

    return items;
}

and call that in your constructor:

private static readonly MainObject myObject = new()
{
  Type = &quot;OP&quot;,
  Group = &quot;FD&quot;,
  Items = GetItems(10)
};

答案3

得分: 0

以下是翻译好的部分:

Items = new List<SubObject>()
{
    new SubObject("Tag.Count", 0),
    new SubObject("Tag.Min", 0),
    new SubObject("Tag.Max", 100)
}
.Concat(Enumerable.Range(1, count).Select(x => new SubObject($"Tag.Pos.{i}", x*x)))
.ToList();

You can Concat to your list and create a new one in one statement.

Whether this is more readable than the other solutions is up to you. If it is more complex than your example here, an extra function as suggested in the other answer might be preferable for readability purposes.

英文:
Items = new List&lt;SubObject&gt;()
{
    new SubObject(&quot;Tag.Count&quot;, 0),
    new SubObject(&quot;Tag.Min&quot;, 0),
    new SubObject(&quot;Tag.Max&quot;, 100)
}
.Concat(Enumerable.Range(1, count).Select(x =&gt; new SubObject($&quot;Tag.Pos.{i}&quot;, x*x))))
.ToList();

You can Concat to your list and create a new one in one statement.

Whether this is more readable then the other solutions is up to you. If it is more complex then your example here, an extra function as suggested in the other answer might be preferable for readability purposes.

huangapple
  • 本文由 发表于 2023年5月22日 22:22:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76307175.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定