IEnumerable集合类型,获取空值
本文关键字:获取 空值 类型 集合 集合类 IEnumerable | 更新日期: 2023-09-27 18:13:36
在StackOverflow今天的帮助下,我已经从我的XML文件得到了我的数据层构造返回数据到我的业务逻辑层。但是,我似乎无法从业务对象层获取数据。这些值都是空的。抱歉,作为一个新手....提前谢谢。
业务逻辑层:
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;
}
这一行:
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。