Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,
i am trying to filter the nodes and store values in a List basesd on the search criteria. My problem is, in some nodes the where clause applied field(AccruedInterest) is missing. How can i overcome this, please help
C#
public static List<AccruedClass> validaccrued()
        {
            XDocument doc = XDocument.Load("Morgan.xml");
            var ac = doc.Root.Elements("Positions")
                          .Where(item => (float)item.Element("AccruedInterest") > 0.0)//some nodes doesnot contain "AccruedInterest" so its showing null error execption
                          .Select(item => new AccruedClass
                          {
                              Cusip = item.Element("Cusip").Value != null ? item.Element("Cusip").Value : "",
                              // Symbol = item.Element("symbol").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,                             
                          })
                          .ToList();
            List<AccruedClass> list_accr = ac.ToList<AccruedClass>();
            return list_accr;
        }
Posted

1 solution

Instead of using float, try using nullable float?.

Something like
C#
...
.Where(item => (float?)item.Element("AccruedInterest") != null && (float?)item.Element("AccruedInterest") > 0.0)
...
 
Share this answer
 
Comments
jinesh sam 17-Jul-15 5:32am    
Thanks Mika :-) Its works fine. float? is new to me. need to google
Wendelius 17-Jul-15 5:42am    
Glad it helped :)


Here's something to start with nullable types: https://msdn.microsoft.com/en-us/library/1t3y8s4s.aspx[^]
jinesh sam 17-Jul-15 15:34pm    
if you are free please help on this question
http://www.codeproject.com/Questions/1010662/not-able-to-find-elements-with-Descendants-key-wor

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