Chris Lombardi
Chris Lombardi

Reputation: 891

Do not create XElement when value is NULL

I have the below code which is working, however; I want to omit XElements that have no value and have been unable to figure this out. Any suggestions would be helpful.

        foreach (DataRow row in ds.Tables[2].Rows)
        {

            result.Add(new XElement(new XElement("action",
                                             new XElement("actionType", row["actionType"].ToString()),
                                             new XElement("originalUnits", row["originalUnits"].ToString()),
                                             new XElement("adjustedUnits", row["adjustedUnits"].ToString()),
                                             new XElement("adjustmentPercentage", row["adjustmentPercentage"].ToString()),
                                             new XElement("userMessageCode", row["userMessageCode"].ToString()),
                                             new XElement("review", row["review"].ToString())


                                             )));


        }

UPDATE:

The below code compiles but the empty elements are still present.

       result.Add(new XElement(new XElement("action",
                                         new XElement("actionType", row["actionType"].ToString()),
                                         row["originalUnits"].ToString() != null ? new XElement("originalUnits", row["originalUnits"].ToString()) : null,
                                         row["adjustedUnits"].ToString() != null ? new XElement("adjustedUnits", row["adjustedUnits"].ToString()) : null,
                                         new XElement("adjustmentPercentage", row["adjustmentPercentage"].ToString()),
                                         new XElement("userMessageCode", row["userMessageCode"].ToString()),
                                         new XElement("review", row["review"].ToString())


                                         )));

Upvotes: 0

Views: 91

Answers (1)

Anu Viswan
Anu Viswan

Reputation: 18155

You need to compare the value with DBNull.Value instead of null For example,

result.Add(new XElement(new XElement("action",
                                         new XElement("actionType", row["actionType"].ToString()),
                                         row["originalUnits"] != DBNull.Value ? new XElement("originalUnits", row["originalUnits"].ToString()) : null,
                                         row["adjustedUnits"] != DBNull.Value ? new XElement("adjustedUnits", row["adjustedUnits"].ToString()) : null,
                                         new XElement("adjustmentPercentage", row["adjustmentPercentage"].ToString()))));

DBNull represents a nonexistent value returned from the database. This is different from null

Do not confuse the notion of null in an object-oriented programming language with a DBNull object. In an object-oriented programming language, null means the absence of a reference to an object. DBNull represents an uninitialized variant or nonexistent database column.

Upvotes: 1

Related Questions