简体   繁体   English

如果类型属性等于 null 或 0,则返回 false

[英]Return false if type properties equals null or 0

I tried following the method as follows here: Checking if Object has null in every property .我尝试按照以下方法操作: Checking if Object has null in every property However, when instantiating Order newOrder = new Order();然而,当实例化Order newOrder = new Order(); . . I cannot simple just implement bool props = newOrder.ArePropertiesNotNull() .我不能简单地只实现bool props = newOrder.ArePropertiesNotNull() What am I supposed to add to my Order class?我应该在我的订单类中添加什么? And where do I implement the function for ArePropertiesNotNull<T>(this T obj) ?我在哪里实现ArePropertiesNotNull<T>(this T obj) I would like to know if there is a way to return false if value returned equals 0 or null?我想知道如果返回的值等于 0 或 null,是否有办法返回 false?

Here is my code:这是我的代码:

OrderProdRepository.cs OrderProdRepository.cs

...
public bool ReadFromFile(string _date)
        {
            taxesFile.ReadFile();
            productsFile.ReadFile();

            string orderFileName = $"C:\\tempfolder\\Orders_{_date}.txt";

            List<string> lines = File.ReadAllLines(orderFileName).ToList();

            foreach (var line in lines.Skip(1)) //?? new List<string>(0)
            {
                List<string> entry = line.Split(',').ToList();

                Order newOrder = new Order();               
                int.TryParse(entry[0], out int orderNumber);
                newOrder.OrderNumber = orderNumber;
                newOrder.Date = _date;
                newOrder.CustomerName = entry[1];
                newOrder.State = taxesFile.StateAbbreviation(entry[2]);
                newOrder.StateName = taxesFile.StateName(newOrder.State);
                decimal.TryParse(entry[3], out decimal taxRate);
                newOrder.TaxRate = taxesFile.TaxRate(taxRate);
                newOrder.ProductType = productsFile.ProductType(entry[4]);
                decimal.TryParse(entry[5], out decimal area);
                newOrder.Area = area;
                decimal.TryParse(entry[6], out decimal costPerSquareFoot);
                newOrder.CostPerSquareFoot = productsFile.CostPerSquareFoot(costPerSquareFoot);
                decimal.TryParse(entry[7], out decimal laborCostPerSquareFoot);
                newOrder.LaborCostPerSquareFoot = productsFile.LaborCostPerSquareFoot(laborCostPerSquareFoot);
                decimal.TryParse(entry[8], out decimal materialCost);
                newOrder.MaterialCost = materialCost;
                decimal.TryParse(entry[9], out decimal laborCost);
                newOrder.LaborCost = laborCost;
                decimal.TryParse(entry[10], out decimal tax);
                newOrder.Tax = tax;
                decimal.TryParse(entry[11], out decimal total);
                newOrder.Total = total;


                orderList.Add(newOrder);
            }
            return true;
        }
...

You need to create this method an extension method.您需要将此方法创建为扩展方法。 It should be defined in static class:它应该在静态类中定义:

public static class ObjectExtensions
{        
    public static bool ArePropertiesNotNull<T>(this T obj)
    {
        return typeof(T).GetProperties().All(propertyInfo => propertyInfo.GetValue(obj) != null);
    }
}

I think you need a function to check each line for null and/or 0 values:我认为您需要一个函数来检查每行是否为null和/或0值:

private bool IsValidLine(string line)
{
    if (line == null)
        return false;

    var arr = line.Split(',');   

    //Uncomment this if splitting the line will always return 11 items array.
    //if (arr.Length < 11)
    //    return false;      

    return arr.Aggregate(0, (n, s) => 
    (decimal.TryParse(s, out decimal d) && d == 0) || 
    string.IsNullOrWhiteSpace(s) ? n + 1 : n) == 0;
}

You can use it in your code as follows:您可以在代码中使用它,如下所示:

public bool ReadFromFile(string _date)
{
    var orderFileName = $"C:\\tempfolder\\Orders_{_date}.txt";
    var lines = File.ReadAllLines(orderFileName);

    foreach (var line in lines.Skip(1))
    {
        //If parsing any line returns false.
        if (!IsValidLine(line))
            return false;

        //Or if you need to create a list of the valid Order entries.
        if (IsValidLine(line))
        {
            var order = new Order();

            //...

            orderList.Add(newOrder);
        }
    }
    return true;
}

Alternatives:备择方案:

  • Add a static function in the Order class to parse a given line and return a new object of Order type if the line is valid.Order类中添加一个静态函数来解析给定的行,如果该行有效,则返回一个Order类型的新对象。 Something like this .这样的东西。
  • If its not too late, then consider using a local database or serialization.如果还不算太晚,请考虑使用本地数据库或序列化。 Something like this and maybe this if you don't mind a vb.net example.事情是这样,也许这个,如果你不介意vb.net的例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM