C# IEnumerable集合类型,获取空值
今天,在StackOverflow的帮助下,我得到了构建的数据层,并将数据从XML文件返回到了业务逻辑层。但是,我似乎无法从业务对象层获取数据。这些值都是空的。对不起,这么一个新手。。。。提前谢谢 业务逻辑层:C# IEnumerable集合类型,获取空值,c#,.net,winforms,C#,.net,Winforms,今天,在StackOverflow的帮助下,我得到了构建的数据层,并将数据从XML文件返回到了业务逻辑层。但是,我似乎无法从业务对象层获取数据。这些值都是空的。对不起,这么一个新手。。。。提前谢谢 业务逻辑层: public void getCustDetails(string customerId) { DLGetCustomers obj = new DLGetCustomers(); obj.getCustDetails(
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;
}
公共类帐户详细信息
{
公共字符串Fname{get;set;}
公共字符串Lname{get;set;}
公共字符串Ssn{get;set;}
公共字符串Dob{get;set;}
公共字符串Custid{get;set;}
}
public IEnumerable getCustDetails(字符串customerId)
{
//提取所选客户的客户信息
var doc=XDocument.Load(“Portfolio.xml”);
var custRecords=来自单据子体(“账户”)中的账户
让账户=账户要素(“账户”)
其中(字符串)account.Attribute(“custid”).Value==customerId
选择新帐户详细信息
{
Fname=(字符串)account.Attribute(“Fname”).Value,
Lname=(字符串)account.Attribute(“Lname”).Value,
Ssn=(字符串)account.Attribute(“Ssn”).Value,
Dob=(字符串)account.Attribute(“Dob”).Value,
Custid=(字符串)account.Attribute(“Custid”).Value
};
归还保管记录;
}
此行:
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。哪些值为null?您的XML文件是什么样子的?