none
How to set nullable property to anonymous RRS feed

  • Question

  • How do i set the properties of "result" to nullable int. The code below doesnt work

    <Package>
      <Document>
        <ID>0</ID>
        <UID/>
        <SequenceNumber>0</SequenceNumber>
      </Document>
      <Document>
        <ID/>
        <UID/>
        <SequenceNumber>1</SequenceNumber>
      </Document>
      <Document>
        <ID>2</ID>
        <UID/>
        <SequenceNumber/>
      </Document>
    </Package>

    static void Main(string[] args) { var result = XDocument.Parse(GetXml()).Root.Elements() .Select(x => new {

    //this doesnt work ID = (int?)(x.Element("ID")), UID = (int?)(x.Element("UID")), SequenceNumber = (int?)(x.Element("SequenceNumber")), }).ToList(); }



    • Edited by lax4u Thursday, October 30, 2014 10:29 PM
    Thursday, October 30, 2014 10:28 PM

Answers

  • Hi,

    have you tried somthing like this

    var result = XDocument.Parse(GetXml()).Root.Elements()
      .Select(x => new
      {
        ID = string.IsNullOrEmpty(x.Element("ID").Value) ? new Nullable<int>() : new Nullable<int>(int.Parse(x.Element("ID").Value)),
        UID = string.IsNullOrEmpty(x.Element("UID").Value) ? new Nullable<int>() : new Nullable<int>(int.Parse(x.Element("UID").Value)),
        SequenceNumber = string.IsNullOrEmpty(x.Element("SequenceNumber").Value) ? new Nullable<int>() : new Nullable<int>(int.Parse(x.Element("SequenceNumber").Value)),
      }).ToList();


    Chris

    Code Samples: Code Samples
    Chrigas Blog: Chrigas Blog

    Thursday, October 30, 2014 11:12 PM