locked
Linq query to accept null value RRS feed

  • Question

  • User-205341453 posted

    How do I get the follwoing query to accept null values for first name and or last name? 

     

     var contacts = from contact in parsedXmlResponse.Descendants("Contacts")
                                       select new
                                       {
                                           Id = contact.Element("ContactRecID").Value,
                                           Name = contact.Element("FirstName").Value + " " + contact.Element("LastName").Value
                                       };


     

    Friday, February 4, 2011 4:01 PM

Answers

  • User1224194097 posted

    You can check if the element is null before retrieving the value

    var contacts = from contact in parsedXmlResponse.Descendants("Contacts")
                    select new
                    {
                        Id = contact.Element("ContactRecID").Value,
                        Name = String.Format("{0} {1}",
                                            contact.Element("FirstName") != null ? contact.Element("FirstName").Value : "",
                                            contact.Element("LastName") != null ? contact.Element("LastName").Value : "")
                    };

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 4, 2011 4:27 PM
  • User-205341453 posted

    Thank you, another way I did it was like so

    var contacts = (from contact in parsedXmlResponse.Descendants("Contact") select new { Id = contact.Element("ContactRecID").Value, Name = ((contact.Element("FirstName") != null ? contact.Element("FirstName").Value : "") + ", " + (contact.Element("LastName") != null ? contact.Element("LastName").Value : "")) }).Distinct();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 4, 2011 4:47 PM

All replies

  • User1224194097 posted

    You can check if the element is null before retrieving the value

    var contacts = from contact in parsedXmlResponse.Descendants("Contacts")
                    select new
                    {
                        Id = contact.Element("ContactRecID").Value,
                        Name = String.Format("{0} {1}",
                                            contact.Element("FirstName") != null ? contact.Element("FirstName").Value : "",
                                            contact.Element("LastName") != null ? contact.Element("LastName").Value : "")
                    };

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 4, 2011 4:27 PM
  • User1835330922 posted

    It is not clear what you want to achieve. Do you have "Contacts" elements that don't have a "FirstName" and/or "LastName" child element? And if so, which value would you want to assign to the "Name" property in that case?

    Perhaps

      Name = ((string)contact.Element("FirstName")??"") + " " + ((string).Element("LastName")??"")

    does what you want (see http://msdn.microsoft.com/en-us/library/ms173224.aspx). But it would assign a string with a single space if both "FirstName" and "lastName" do not exist. Please explain/show how your XML looks and how you want the "Name" property to look.

    Friday, February 4, 2011 4:28 PM
  • User-205341453 posted

    Thank you, another way I did it was like so

    var contacts = (from contact in parsedXmlResponse.Descendants("Contact") select new { Id = contact.Element("ContactRecID").Value, Name = ((contact.Element("FirstName") != null ? contact.Element("FirstName").Value : "") + ", " + (contact.Element("LastName") != null ? contact.Element("LastName").Value : "")) }).Distinct();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 4, 2011 4:47 PM