2013-11-22 34 views
0

我有一个查询,应从返回一个5行,其中使用linq2xml在Windows手机应用程序查询我尝试子句LINQ查询使用其中的Windows Phone应用程序条款

XDocument loadedCustomData = XDocument.Load("pincodes.xml");` 

var filteredData = from c in loadedCustomData.Descendants("record")` 

       where c.Attribute("PON").Equals ("Adapur")` 
       select new pincodes1() 
{ 
    PON = c.Attribute("PostOfficeName").Value, 
    PIN = c.Attribute("Pincode").Value, 
    DIS = c.Attribute("Districts").Value, 
    CT = c.Attribute("City").Value, 
    ST = c.Attribute("State").Value 
}; 
listBox1.ItemsSource = filteredData; 

但随后它给系统的空异常有啥错在查询 这里的截图 here it is where i get error(exception)

,这是类文件

namespace pincodes 
{ 
    class pincodes1 
    { 

     string PostOfficeName; 
     string Pincode; 
     string Districts; 
     string City; 
     string State; 
     public string PON 
     { 
      get { return PostOfficeName; } 
      set { PostOfficeName = value; } 
     } 
     public string PIN 
     { 
      get { return Pincode; } 
      set { Pincode = value; } 
     } 
     public string DIS 
     { 
      get { return Districts; } 
      set { Districts = value; } 
     } 
     public string CT 
     { 
      get { return City; } 
      set { City = value; } 
     } 
     public string ST 
     { 
      get { return State; } 
      set { State = value; } 
     } 
    } 
} 

那么,什么是在LINQ查询

+0

什么是内部异常? – Damith

+0

我附上截图,它给出了错误,你可以检查它 – SD7

回答

2

系统空异常的原因,当你得到一个属性的值,如果它是空,你会得到异常,可以做如下

var filteredData = 
    from c in loadedCustomData.Descendants("record") 
    where (string)c.Attribute("PON") == "Adapur" 
    select new pincodes1() 
    { 
     PON = (string)c.Attribute("PostOfficeName"), 
     PIN = (string)c.Attribute("Pincode"), 
     DIS = (string)c.Attribute("Districts"), 
     CT = (string)c.Attribute("City"), 
     ST = (string)c.Attribute("State") 
    }; 
+0

我得到哪里条款声明的错误,详情请看附件屏幕截图 – SD7

+1

@SmartDeveloper检查我的更新 – Damith