IEnumerable集合类型,获取空值

时间:2011-11-06 19:47:27

标签: c# .net winforms

在今天的StackOverflow的帮助下,我已经将我的数据层构建的返回数据从我的XML文件返回到我的业务逻辑层。但是,我似乎无法从业务对象层获取数据。值均为null。对不起,要成为这样的新手....提前致谢。

业务逻辑层:

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 个答案:

答案 0 :(得分:2)

这一行:

AccountDetails obj1 = new AccountDetails();

只需将obj1设置为AccountDetails的新实例,该实例将充满空字符串。

您可能需要更改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,则需要在使用前检查返回值是否为null。

相关问题