English 中文(简体)
IE 无数收集类型,失去价值
原标题:IEnumerable collection type, getting null values

今天,在StackOverflow的帮助下,我把我的数据层从我的XML档案中修建了返回数据。 然而,我似乎从我的商业目标层面获得数据。 这些价值观完全无效。 Sorry, to be such a newbie... 提前感谢。

3. 商业逻辑层面:

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,该新例将满载空示。

You probably need to change getCustDetails in your DAL to return an instance of AccountDetails instead of an IEnumerable of it, and set obj1 to that:

AccountDetails obj1 = obj.getCustDetails(customerId);

阁下:

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();
} 

Note that if your DAL can t find an account with the specified customerId, it will return a null (which is the default value of a class). You will need to check the return value against null before using if you do not want a NullReferenceException to be thrown in such an eventuality.

问题回答

暂无回答




相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签