2011-11-06 57 views
1

今天在StackOverflow的幫助下,我從我的XML文件中獲取了數據層構造的返回數據到我的業務邏輯層。但是,我似乎無法從我的業務對象層獲取數據。這些值全爲空。對不起,成爲這樣的新手....提前致謝。IEnumerable集合類型,獲取空值

業務邏輯層:

public void getCustDetails(string customerId) 
{       
    DLGetCustomers obj = new DLGetCustomers(); 
    obj.getCustDetails(customerId); 
    AccountDetails obj1 = new AccountDetails(); 
    FirstName = obj1.Fname; 
    LastName = obj1.Lname; 
    SSN = obj1.Ssn; 
    Dob = Convert.ToDateTime(obj1.Dob); 
    CustomerId = Convert.ToInt32(obj1.Custid); 
    TimeSpan ts = DateTime.Now - Convert.ToDateTime(Dob); 
    Age = ts.Days/365; 
} 

數據訪問層:

public class AccountDetails 
{ 
    public string Fname { get; set; } 
    public string Lname { get; set; } 
    public string Ssn { get; set; } 
    public string Dob { get; set; } 
    public string Custid { get; set; } 
} 

public IEnumerable<AccountDetails> getCustDetails(string customerId) 
{ 
    //Pulls customer information for selected customer 
    var doc = XDocument.Load("Portfolio.xml"); 
    var custRecords = from account in doc.Descendants("acct") 
         let acct = account.Element("acct") 
         where (string)account.Attribute("custid").Value == customerId 
         select new AccountDetails 
         { 
          Fname = (string)account.Attribute("fname").Value, 
          Lname = (string)account.Attribute("lname").Value, 
          Ssn = (string)account.Attribute("ssn").Value, 
          Dob = (string)account.Attribute("dob").Value, 
          Custid = (string)account.Attribute("custid").Value 
         };       

    return custRecords; 
} 
+1

什麼值爲空?你的XML文件是怎樣的? –

回答

2

這條線:

AccountDetails obj1 = new AccountDetails(); 

簡單地設置obj1AccountDetails一個新的實例這將是充滿空的字符串。

你可能需要在你的DAL改變getCustDetails返回的AccountDetails,而不是它的一個IEnumerable一個實例,並設置obj1到:

AccountDetails obj1 = obj.getCustDetails(customerId); 

在你的DAL:

public AccountDetails getCustDetails(string customerId) 
{ 
    //Pulls customer information for selected customer 
    var doc = XDocument.Load("Portfolio.xml"); 
    var custRecords = from account in doc.Descendants("acct") 
         let acct = account.Element("acct") 
         where (string)account.Attribute("custid").Value == customerId 
         select new AccountDetails 
         { 
          Fname = (string)account.Attribute("fname").Value, 
          Lname = (string)account.Attribute("lname").Value, 
          Ssn = (string)account.Attribute("ssn").Value, 
          Dob = (string)account.Attribute("dob").Value, 
          Custid = (string)account.Attribute("custid").Value 
         }; 


    return custRecords.FirstOrDefault(); 
} 

請注意,如果您的DAL找不到具有指定的customerId的帳戶,它將返回一個null(這是一個類的默認值)。如果你不想在這種情況下拋出NullReferenceException,你需要在使用前檢查返回值是否爲空。

相關問題