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

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

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

  1. ...
  2. public bool ReadFromFile(string _date)
  3. {
  4. taxesFile.ReadFile();
  5. productsFile.ReadFile();
  6. string orderFileName = "C:\\tempfolder\\Orders_" + _date + ".txt";
  7. List<string> lines = File.ReadAllLines(orderFileName).ToList();
  8. foreach (var line in lines.Skip(1))
  9. {
  10. List<string> entry = line.Split(',').ToList();
  11. Order newOrder = new Order();
  12. int.TryParse(entry[0], out int orderNumber);
  13. newOrder.OrderNumber = orderNumber;
  14. newOrder.Date = _date;
  15. newOrder.CustomerName = entry[1];
  16. newOrder.State = taxesFile.StateAbbreviation(entry[2]);
  17. newOrder.StateName = taxesFile.StateName(newOrder.State);
  18. decimal.TryParse(entry[3], out decimal taxRate);
  19. newOrder.TaxRate = taxesFile.TaxRate(taxRate);
  20. newOrder.ProductType = productsFile.ProductType(entry[4]);
  21. decimal.TryParse(entry[5], out decimal area);
  22. newOrder.Area = area;
  23. decimal.TryParse(entry[6], out decimal costPerSquareFoot);
  24. newOrder.CostPerSquareFoot = productsFile.CostPerSquareFoot(costPerSquareFoot);
  25. decimal.TryParse(entry[7], out decimal laborCostPerSquareFoot);
  26. newOrder.LaborCostPerSquareFoot = productsFile.LaborCostPerSquareFoot(laborCostPerSquareFoot);
  27. decimal.TryParse(entry[8], out decimal materialCost);
  28. newOrder.MaterialCost = materialCost;
  29. decimal.TryParse(entry[9], out decimal laborCost);
  30. newOrder.LaborCost = laborCost;
  31. decimal.TryParse(entry[10], out decimal tax);
  32. newOrder.Tax = tax;
  33. decimal.TryParse(entry[11], out decimal total);
  34. newOrder.Total = total;
  35. orderList.Add(newOrder);
  36. }
  37. return true;
  38. }
  39. ...
英文:

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

  1. ...
  2. public bool ReadFromFile(string _date)
  3. {
  4. taxesFile.ReadFile();
  5. productsFile.ReadFile();
  6. string orderFileName = $&quot;C:\\tempfolder\\Orders_{_date}.txt&quot;;
  7. List&lt;string&gt; lines = File.ReadAllLines(orderFileName).ToList();
  8. foreach (var line in lines.Skip(1)) //?? new List&lt;string&gt;(0)
  9. {
  10. List&lt;string&gt; entry = line.Split(&#39;,&#39;).ToList();
  11. Order newOrder = new Order();
  12. int.TryParse(entry[0], out int orderNumber);
  13. newOrder.OrderNumber = orderNumber;
  14. newOrder.Date = _date;
  15. newOrder.CustomerName = entry[1];
  16. newOrder.State = taxesFile.StateAbbreviation(entry[2]);
  17. newOrder.StateName = taxesFile.StateName(newOrder.State);
  18. decimal.TryParse(entry[3], out decimal taxRate);
  19. newOrder.TaxRate = taxesFile.TaxRate(taxRate);
  20. newOrder.ProductType = productsFile.ProductType(entry[4]);
  21. decimal.TryParse(entry[5], out decimal area);
  22. newOrder.Area = area;
  23. decimal.TryParse(entry[6], out decimal costPerSquareFoot);
  24. newOrder.CostPerSquareFoot = productsFile.CostPerSquareFoot(costPerSquareFoot);
  25. decimal.TryParse(entry[7], out decimal laborCostPerSquareFoot);
  26. newOrder.LaborCostPerSquareFoot = productsFile.LaborCostPerSquareFoot(laborCostPerSquareFoot);
  27. decimal.TryParse(entry[8], out decimal materialCost);
  28. newOrder.MaterialCost = materialCost;
  29. decimal.TryParse(entry[9], out decimal laborCost);
  30. newOrder.LaborCost = laborCost;
  31. decimal.TryParse(entry[10], out decimal tax);
  32. newOrder.Tax = tax;
  33. decimal.TryParse(entry[11], out decimal total);
  34. newOrder.Total = total;
  35. orderList.Add(newOrder);
  36. }
  37. return true;
  38. }
  39. ...

答案1

得分: 0

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

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

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

  1. public static class ObjectExtensions
  2. {
  3. public static bool ArePropertiesNotNull&lt;T&gt;(this T obj)
  4. {
  5. return typeof(T).GetProperties().All(propertyInfo =&gt; propertyInfo.GetValue(obj) != null);
  6. }
  7. }

答案2

得分: 0

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

  1. 我认为你需要一个函数来检查每一行是否包含`null`/`0`值:
  2. private bool IsValidLine(string line)
  3. {
  4. if (line == null)
  5. return false;
  6. var arr = line.Split(',');
  7. //Uncomment this if splitting the line will always return 11 items array.
  8. //if (arr.Length < 11)
  9. // return false;
  10. return arr.Aggregate(0, (n, s) =>
  11. (decimal.TryParse(s, out decimal d) && d == 0) ||
  12. string.IsNullOrWhiteSpace(s) ? n + 1 : n) == 0;
  13. }
  14. 你可以在你的代码中如下使用它:
  15. public bool ReadFromFile(string _date)
  16. {
  17. var orderFileName = $"C:\\tempfolder\\Orders_{_date}.txt";
  18. var lines = File.ReadAllLines(orderFileName);
  19. foreach (var line in lines.Skip(1))
  20. {
  21. //If parsing any line returns false.
  22. if (!IsValidLine(line))
  23. return false;
  24. //Or if you need to create a list of the valid Order entries.
  25. if (IsValidLine(line))
  26. {
  27. var order = new Order();
  28. //...
  29. orderList.Add(newOrder);
  30. }
  31. }
  32. return true;
  33. }

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

英文:

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

  1. private bool IsValidLine(string line)
  2. {
  3. if (line == null)
  4. return false;
  5. var arr = line.Split(&#39;,&#39;);
  6. //Uncomment this if splitting the line will always return 11 items array.
  7. //if (arr.Length &lt; 11)
  8. // return false;
  9. return arr.Aggregate(0, (n, s) =&gt;
  10. (decimal.TryParse(s, out decimal d) &amp;&amp; d == 0) ||
  11. string.IsNullOrWhiteSpace(s) ? n + 1 : n) == 0;
  12. }

You can use it in your code as follows:

  1. public bool ReadFromFile(string _date)
  2. {
  3. var orderFileName = $&quot;C:\\tempfolder\\Orders_{_date}.txt&quot;;
  4. var lines = File.ReadAllLines(orderFileName);
  5. foreach (var line in lines.Skip(1))
  6. {
  7. //If parsing any line returns false.
  8. if (!IsValidLine(line))
  9. return false;
  10. //Or if you need to create a list of the valid Order entries.
  11. if (IsValidLine(line))
  12. {
  13. var order = new Order();
  14. //...
  15. orderList.Add(newOrder);
  16. }
  17. }
  18. return true;
  19. }

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:

确定