Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C#
.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?
})
Posted

1 solution

Why not calculate it in a property in your AccruedClass on demand?
C#
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?
 
Share this answer
 
Comments
jinesh sam 18-Jul-15 3:10am    
Thanks this helps for me. What about null check is there any other option
OriginalGriff 18-Jul-15 3:31am    
Well - there is the null coalescing operator:
https://msdn.microsoft.com/en-us/library/ms173224.aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900