在LINQ中检查null异常的最佳方法 [英] best way to check null exception in LINQ

查看:76
本文介绍了在LINQ中检查null异常的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,

我需要知道LINQ中有没有更好的选项来执行此检查

AccruedInterest = item.Element( AccruedInterest)。值!= null? Convert.ToDouble(item.Element(AccruedInterest)。Value):0,

2nd Question

我需要在里面做一些乘法选择语句。我可以使用定义列名的变量。

请查看代码。

请点亮一下来解决问题。

提前致谢



Hi Friends,
I need to know is there any better option in the LINQ to perform this check
AccruedInterest = item.Element("AccruedInterest").Value != null ? Convert.ToDouble(item.Element("AccruedInterest").Value) : 0,
2nd Question
I need to do some multiplication inside the select statement. Can i use the variable which define the column name.
Please have a look into the code.
Please shed some lights on to solve the issue.
Thanks in advance

.Select(item => new AccruedClass
{
Cusip = item.Element("Cusip").Value != null ? item.Element("Cusip").Value : "",
AccruedInterest = item.Element("AccruedInterest").Value != null ? Convert.ToDouble(item.Element("AccruedInterest").Value) : 0,
PriceFactor = item.Element("PriceFactor").Value != null ? Convert.ToDouble(item.Element("PriceFactor").Value) : 0,
Quantity = item.Element("Quantity").Value != null ? Convert.ToDouble(item.Element("Quantity").Value) : 0,
//CalcAcc =  AccruedInterest * PriceFactor * Quantity    // i need this calculation here. whats the best way?
})

推荐答案

为什么不在AccruedClass的属性中计算它需求?

Why not calculate it in a property in your AccruedClass on demand?
public class AccruedClass
   {
   ...
   public double CalcAcc { get { return AccruedInterest * PriceFactor * Quantity; } }
   }

这样它总是返回当前值,即使构造后更改了数量?

That way it always returns the current value, even if the Quantity is changed after construction?


这篇关于在LINQ中检查null异常的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆