来自4个单独的列表

来自4个单独的列表

本文介绍了创建1个属性列表< object>来自4个单独的列表< T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个C#WPF窗口我插入4 List< t>字符串,十进制,十进制和int。每个列表都正确编号为0,1,2 ...并在列表中按顺序显示正确的信息。我正在尝试创建List< myobject>的公共属性。具有属性myobject.string,myobject.decimal,myobject.decimal,myobject.int的属性如何将这些属性输入到我的对象列表中?以下是我所拥有的以及我尝试过的一些方法。



Hi everyone,
I have a C# WPF window that I insert 4 List<t> a string,decimal,decimal, and int. Each list is numbered correctly 0,1,2… with the correct information in order in the list. I am trying to create a public property of a List<myobject> that has properties myobject.string, myobject.decimal, myobject.decimal, myobject.int How can I enter these properties into my list of my object? Here is what I have and some ways I have been trying.

public partial class ShoppingCart : Window
    {
        private List<string> listProductNames = new List<string>();
        private List<decimal> listProductPrices = new List<decimal>();
        private List<decimal> listProductSalesTax = new List<decimal>();
        private List<int> listProductQuantity = new List<int>();
        public List<Product> Cart => new List<Product>();

        public ShoppingCart(Customer c, List<string> lpn, List<decimal> lpp, List<decimal> lpt, List<int> lpq)
        {
            InitializeComponent();
            customer = c;
            listProductNames = lpn;
            listProductPrices = lpp;
            listProductSalesTax = lpt;
            listProductQuantity = lpq;
            CreateCart(listProductNames, listProductPrices, listProductSalesTax, listProductQuantity);
            PopulateListViewCart();
        }

        private void CreateCart(List<string> lpn, List<decimal> lpp, List<decimal> lpt, List<int> lpq)
        {
            selectedProduct = new Product();

            foreach(string name in lpn)
            {
                selectedProduct.ProductName = name;
            }
            foreach(decimal price in lpp)
            {
                selectedProduct.ProductPrice = price;
            }
            foreach(decimal tax in lpt)
            {
                selectedProduct.ProductTax = tax;
            }
            foreach(int quantity in lpq)
            {
                selectedProduct.ProductQuantity = quantity;
            }
            Cart.Add(selectedProduct);
        }
}





我的尝试:



foreachs单独设置prod并添加到列表



What I have tried:

foreachs setting prod seperately and adding to list

推荐答案

for (int i = 0; i < lpn.Count; i++)
{
    Product selectedProduct = new Product();
    selectedProduct.ProductName = lpn[i];
    selectedProduct.ProductPrice = lpp[i];
    // etc
    Cart.Add(selectedProduct);
}


public Product GetProduct( int n ) {

   Product p = new Product();

   p.ProductName = this.listProductNames[ n ];
   // etc.

   return p;
}


这篇关于创建1个属性列表&lt; object&gt;来自4个单独的列表&lt; T&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 05:59