2011-10-25 58 views
0

我試圖構建一個查詢,該查詢返回一個包含基於客戶ID選擇的客戶信息的集合。當用戶從組合框(cboCustomer)中選擇一個客戶時,選定的索引更改事件開始,下面的方法包含查詢。不幸的是,查詢不起作用。當我將鼠標懸停在custRecord * 強文本 *上的結果上時,我收到錯誤「未將對象引用設置爲對象的實例」。Linq to XML:XML查詢(.Net Windows窗體應用程序)

在此先感謝新手到XML!

xml文件粘貼在下面;所有數據都完全組成。

private void cboCustomer_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     var doc = XElement.Load("Portfolio.xml"); 
     customerId = cboCustomer.SelectedIndex.ToString(); 
     if (cboCustomer.SelectedIndex != 0) 
     {      
      var custRecord = from account in doc.Descendants("account") 
       let acct = account.Element("acct") 
       where (string)account.Attribute("custid").Value == customerId 
       select new 
       {Fname=(string)account.Attribute("fname").Value, 
        Lname=(string)account.Attribute("lname").Value, 
        Ssn = (string)account.Attribute("ssn").Value, 
        Dob = (string)account.Attribute("dob").Value}; 
     } 
    } 
<?xml version="1.0" encoding="utf-8" ?> 
<portfolio> 
    <account> 
    <acct custid="1" fname="Tommy" lname="Hawk" ssn="928-329-1929" dob="4/6/1988"></acct> 
    <deposits depid="1000" depdate="1/2/2011" depamount="1350.53"></deposits> 
    <deposits depid="1003" depdate="2/3/2011" depamount="1377.81"></deposits> 
    <deposits depid="1008" depdate="3/14/2011" depamount="84.00"></deposits> 
    <withdrawals wdid="2001" wddate="1/31/2011" wdamount="80.00"></withdrawals> 
    <withdrawals wdid="2005" wddate="4/8/2011" wdamount="80.00"></withdrawals> 
    <withdrawals wdid="2007" wddate="6/1/2011" wdamount="2600.00"></withdrawals> 
    </account> 
    <account> 
    <acct custid="2" fname="I. P." lname="Nightly" ssn="457-23-4871" dob="6/1/1945"></acct> 
    <deposits depid="1004" depdate="2/8/2011" depamount="741.22"></deposits> 
    <deposits depid="1005" depdate="2/9/2011" depamount="47.00"></deposits> 
    <deposits depid="1009" depdate="3/14/2011" depamount="89.99"></deposits> 
    <withdrawals wdid="2003" wddate="3/1/2011" wdamount="55.00"></withdrawals> 
    <withdrawals wdid="2004" wddate="3/3/2011" wdamount="28.00"></withdrawals> 
    <withdrawals wdid="2006" wddate="4/8/2011" wdamount="450.00"></withdrawals> 
    </account> 
    <account> 
    <acct custid="3" fname="Mary" lname="Echmass" ssn="192-01-2933" dob="8/10/1973"></acct> 
    <deposits depid="1002" depdate="1/15/2011" depamount="841.77"></deposits> 
    <deposits depid="1006" depdate="2/14/2011" depamount="2170.00"></deposits> 
    <deposits depid="1007" depdate="3/10/2011" depamount="21.01"></deposits> 
    <withdrawals wdid="2002" wddate="1/16/2011" wdamount="700.00"></withdrawals> 
    <withdrawals wdid="2008" wddate="6/3/2011" wdamount="24.00"></withdrawals> 
    <withdrawals wdid="2009" wddate="6/30/2100" wdamount="38.46"></withdrawals> 
    </account> 
</portfolio> 
+1

」不幸的是,查詢不起作用。「這不是對問題的充分描述 - 這相當於去看醫生並說「我感覺不舒服」。請提供更多關於您的預期結果和獲得的結果的詳細信息,請參閱http://tinyurl.com/so-hints –

回答

1

「客戶ID」是「會計學」的屬性,而不是「賬戶」,讓您的Where子句從來沒有發現任何東西。對於這個問題,你的其他屬性也是如此。 「

相關問題