从类中获取列表返回为空[关闭]

问题描述 投票:0回答:1

我正在尝试从另一个类中获取public list,但是在获取列表时,它返回为null。调试时,列表已正确添加,但是返回为null。

这是我的代码:

OrderAddToListWorkflow.cs

Console.Clear();

OrderManager manager = OrderManagerFactory.Create();

Console.WriteLine("Add an Order");
Console.WriteLine("--------------------");
Console.WriteLine("Enter Order Number: ");
int orderNumber = int.Parse(Console.ReadLine());
Console.WriteLine("Enter future date: ");
string date = Console.ReadLine();

OrderAddToListResponse newresponse = manager.ReadFile(orderNumber, date);
ProductsFile productsClassFile = new ProductsFile();
var myList = productsClassFile.productsFile.Select(x => x.ProductsFile).ToList(); //returns as null

Console.WriteLine("Enter Customer Name: ");
string customerName = Console.ReadLine();
Console.WriteLine("Enter State: ");
string state = Console.ReadLine();
Console.WriteLine("Enter Tax Rate: ");
decimal taxRate = decimal.Parse(Console.ReadLine());
for(int i = 0; i < myList.Count; i++)
{
    Console.WriteLine(myList[i]);
}

Console.WriteLine("Enter Product Type: ");
string productType = Console.ReadLine();
Console.WriteLine("Enter Area: ");
decimal area = decimal.Parse(Console.ReadLine());
Console.WriteLine("Enter Cost Per Square: ");
decimal costPerSquareFoot = decimal.Parse(Console.ReadLine());
Console.WriteLine("Enter Labor Cost Per Square Foot: ");
decimal laborCostPerSquareFoot = decimal.Parse(Console.ReadLine());

ProductsFile.cs

public List<Order> productsFile = new List<Order>();

    public void ReadFile()
    {
        string productsFileName = $"C:\\tempfolder\\Products.txt";
        List<string> productsLines = File.ReadAllLines(productsFileName).ToList();

        foreach (var productLine in productsLines.Skip(1))
        {
            List<string> entry = productLine.Split(',').ToList();

            Order productsOrder = new Order();
            productsOrder.ProductType = entry[0];
            decimal.TryParse(entry[1], out decimal _costPerSquareFoot);
            productsOrder.CostPerSquareFoot = _costPerSquareFoot;
            decimal.TryParse(entry[2], out decimal _laborCostPerSquareFoot);
            productsOrder.LaborCostPerSquareFoot = _laborCostPerSquareFoot;
            var _product = productsOrder.ProductsFile;

            productsFile.Add(productsOrder);
        }
    }

Order.cs

public class Order
{
    public int OrderNumber { get; set; }
    public string Date { get; set; }
    public string CustomerName { get; set; }
    public string State { get; set; }
    public string StateName { get; set; }
    public decimal TaxRate { get; set; }
    public string ProductType { get; set; }
    public decimal Area { get; set; }
    public decimal CostPerSquareFoot { get; set; }
    public decimal LaborCostPerSquareFoot { get; set; }
    public decimal MaterialCost { get; set; }
    public decimal LaborCost { get; set; }
    public decimal Tax { get; set; }
    public decimal Total { get; set; }


    public string TaxFile => $"{State},{StateName},{TaxRate}";
    public string ProductsFile => $"{ProductType},{CostPerSquareFoot},{LaborCostPerSquareFoot}";

    public Order() { }
...

这里是一个快照或调试属性:

Enter image description here

在这里我看到_orderRepository具有产品列表文件列表。

Enter image description here

但是为什么不能将字符串类型的ProductsFile存储到列表中?

Enter image description here

c#
1个回答
1
投票

从外观上,你失踪了productsClassFile.ReadFile();

之间

ProductsFile productsClassFile = new ProductsFile();
// insert here
var myList = productsClassFile.productsFile.Select(x => x.ProductsFile).ToList(); //returns as null
© www.soinside.com 2019 - 2024. All rights reserved.