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

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

Return false if type properties equals null or 0

问题

I tried following the method as follows here: link. However, when instantiating Order newOrder = new Order();. I cannot simply just implement 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)? I would like to know if there is a way to return false if the value returned equals 0 or null?

Here is my code:

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))
    {
        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;
}
...
英文:

I tried following the method as follows here: https://stackoverflow.com/questions/50128815/checking-if-object-has-null-in-every-property . However, when instantiating Order newOrder = new Order();. I cannot simple just implement bool props = newOrder.ArePropertiesNotNull(). What am I supposed to add to my Order class? And where do I implement the function for ArePropertiesNotNull&lt;T&gt;(this T obj)? I would like to know if there is a way to return false if value returned equals 0 or null?

Here is my code:

OrderProdRepository.cs

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

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

            List&lt;string&gt; lines = File.ReadAllLines(orderFileName).ToList();

            foreach (var line in lines.Skip(1)) //?? new List&lt;string&gt;(0)
            {
                List&lt;string&gt; entry = line.Split(&#39;,&#39;).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;
        }
...

答案1

得分: 0

你需要将这个方法创建为扩展方法。它应该被定义在静态类中:

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

You need to create this method an extension method. It should be defined in static class:

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

答案2

得分: 0

以下是已翻译的代码部分:

我认为你需要一个函数来检查每一行是否包含`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;
}

你可以在你的代码中如下使用它:

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;
}

希望这对你有帮助。如果你需要更多帮助,请随时提问。

英文:

I think you need a function to check each line for null and/or 0 values:

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

    var arr = line.Split(&#39;,&#39;);   

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

    return arr.Aggregate(0, (n, s) =&gt; 
    (decimal.TryParse(s, out decimal d) &amp;&amp; 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 = $&quot;C:\\tempfolder\\Orders_{_date}.txt&quot;;
    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. 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.

huangapple
  • 本文由 发表于 2020年1月4日 12:58:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/59588026.html
匿名

发表评论

匿名网友

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

确定