【问题标题】:Add an existing list item to another list将现有列表项添加到另一个列表
【发布时间】:2012-10-30 15:52:55
【问题描述】:

好的,为了解释这一点,我将尝试总结一下我在做什么

有时我会在“供应商”类中创建一个项目列表。在此示例中,将零件列表添加到主类中存在的供应商列表中。

然后,我想选择一个特定的部分添加到作业(作业类),这个部分已经创建,我只是想把它添加到作业中。

该部分已使用以下内容添加: '在此之前,供应商已被选中'

Class Supplier
public void AddParts( int PartNum, string PartName, string PartDescription, decimal      Cost, decimal CostWithVAT, decimal Price, short Quantity)
{          
    m_partUsed.Add(new Part(PartNum, PartName, PartDescription, Cost, Price, Quantity));         
}

这是我打算如何实现的:

   private void btnAddJobPart_Click(object sender, EventArgs e)
    {
        //Select the job that the part is to be added too
        string selectedJob = cboJobPartRef.Text;
        List<Job> foundJob = Program.AuspexDo.SelectJob(selectedJob);

        //For the job found
        foreach (Job j in foundJob)
        {
            //Select the supplier
            string selectedSupplier = cboJobSupplier.Text;
            List<Supplier> foundSup = Program.AuspexDo.SelectSupplier(selectedSupplier);

            //For the supplier found, find the part to be added
            foreach (Supplier s in foundSup)
            {
                string selectedPart = cboJobParts.Text;
                List<Part> foundPart = s.SelectPart(selectedPart);

                //Get those part details

                //Add those details to the job

                j.AddParts //the part item here from the supplier list;


            }                   
        }
    }

任何帮助将不胜感激。谢谢。

【问题讨论】:

  • 那么,问题到底是什么?
  • 那么什么不起作用?这个问题的哪一方面你不知道怎么办?您当前的解决方案距离解决问题有多近?
  • 不太清楚这里要问什么...您在实现此代码时遇到任何问题吗?
  • 我不知道你的代码打算做什么,但每次在 foreach 中分配给 foundSupfoundPart 对我来说都是错误的。
  • 不确定我是否理解您要执行的操作(如果不查看代码,很难理解供应商列表中的部件项目是什么),但看起来您可以对所有供应商执行一些 SelectMany零件和 AddRange。

标签: c# list oop


【解决方案1】:

我想你在找List.AddRange

m_partUsed.AddRange(foundPart)

【讨论】:

    【解决方案2】:

    为什么不直接将AddParts 方法更改为(并称其为AddPart,因为它一次只添加一个部分):

    public void AddPart(Part part)
    {          
        m_partUsed.Add(part);         
    }
    

    然后你可以添加一个部分

    j.AddPart(somePart);
    

    或使用对象初始化器

    j.AddPart(new Part{Num=PartNum, Name=PartName, Description=PartDescription,
                       Cost=Cost, Price=Price, Quantity=Quantity});
    

    或使用构造函数

    j.AddPart(new Part(PartNum, PartName, PartDescription,
                       Cost, Price, Quantity));
    

    如果您希望保留原始实现,您可以并排使用两个 AddPart 方法。这称为方法重载。然后,您可以选择添加零件对象或单个零件值。

    public void AddPart(Part part) { ... }
    public void AddPart(int PartNum, string PartName,
                        string PartDescription, decimal Cost,
                        decimal CostWithVAT, decimal Price, short Quantity) { ... }
    

    只要参数列表不同,您可以拥有任意多个具有相同名称的方法。参数的数量必须不同,或者参数类型必须不同。不考虑参数名称。

    【讨论】:

    • 我觉得第一位是我需要去的地方,但我遇到的问题是它说我无法通过 AddPart 参数传递列表项。稍后我将不得不回到这个问题,我整个下午都在尝试,我敢肯定在休息后你所放的东西就会到位,我可以让它工作。谢谢。
    • 看来List&lt;Part&gt; foundPart = s.SelectPart(selectedPart); 的说法是错误的。难道不是Part foundPart = s.SelectPart(selectedPart);吗?
    • 我不这么认为,选择部分返回列表:public List&lt;Part&gt; SelectPart(String PartRef) { List&lt;Part&gt; PartSet = new List&lt;Part&gt;(); foreach (Part p in m_supplierParts) { if (p.PartNumber.ToString().Equals(PartRef, StringComparison.OrdinalIgnoreCase)) PartSet.Add(p); } return PartSet; }
    • 那么应该叫SelectParts
    【解决方案3】:

    AddParts 方法创建一个新部件。我将更改此方法,使其采用 Part 参数,然后将其添加到列表中。

    public void AddParts(Part p)
    {          
        m_partUsed.Add(p);         
    }
    

    【讨论】:

      【解决方案4】:

      你也可以这样做:

      j.AddParts(foundSup.SelectMany(s => s.SelectPart(selectedPart)))
      

      虽然 AddParts 看起来像 Alexei 的答案。

      【讨论】:

        【解决方案5】:

        如果我正确理解 OP,他想将列表添加到列表中,...

        好吧,我不了解 C#,但我了解 Unity C#

        我很快就给你做了:

        using UnityEngine;
        using System.Collections;
        using System.Collections.Generic; // THIS one is very important since it contains the List
        
        public class TestScript : MonoBehaviour {
            List<int> ListOfIntegers = new List<int>();
            List<int> ListOfIntegers1 = new List<int>();
            List<List<int>> ListOfLists = new List<List<int>>();
        
        
            void Start(){
                ListOfIntegers.Add(1);
                ListOfIntegers1.Add(10);
                ListOfLists.Add(ListOfIntegers1);
                ListOfLists.Add(ListOfIntegers);
                Debug.Log((ListOfLists[0])[0]); // you get debug 10
            }
        }
        

        我检查过它是否正确读取;)

        我希望这是有用的。到非 Unity C#

        【讨论】:

          猜你喜欢
          • 2021-08-18
          • 2019-09-22
          • 2022-06-13
          • 1970-01-01
          • 2020-01-23
          • 2013-03-25
          • 1970-01-01
          • 1970-01-01
          • 2018-03-06
          相关资源
          最近更新 更多