It is necessary to take a refresher course on access to property

I need help accessing class properties in this class.

For example, take the class below:

public partial class Account
    {
        private Profile _profile;
        private Email _email;
        private HostInfo _hostInfo;

        public Profile Profile
        {
            get { return _profile; }
            set { _profile = value; }
        }

        public Email Email
        {
            get { return _email; }
            set { _email = value; }
        }

        public HostInfo HostInfo
        {
            get { return _hostInfo; }
            set { _hostInfo = value; }
        }

In the Account class, there are a bunch of class properties, such as Email or Profile. Now that I want to access these properties at runtime, I am doing something like this (for email):

    _accountRepository = ObjectFactory.GetInstance<IAccountRepository>();
    string username = Cryptography.Decrypt(_webContext.UserNameToVerify, "verify");
    Account account = _accountRepository.GetAccountByUserName(username);

    if(account != null)
    {
        account.Email.IsConfirmed = true;

But, I get "Object reference not set ..." for account.Email ... Why? How can I access an account like account.Email, account.Profile etc. returns the correct data for a given AccountId or UserName.

    Here is a method that returns Account:

public Account GetAccountByUserName(string userName)
{
    Account account = null;

    using (MyDataContext dc = _conn.GetContext())
    {
        try
        {
            account = (from a in dc.Accounts
                       where a.UserName == userName
                       select a).FirstOrDefault();
        }
        catch
        {
            //oops
        }
    }

    return account;

}

The above works, but when I try:

 account = (from a in dc.Accounts
               join em in dc.Emails on a.AccountId equals em.AccountId
               join p in dc.Profiles on em.AccountId equals p.AccountId
               where a.UserName == userName
               select a).FirstOrDefault();


. SQL - , Account?

!

+3
4

- , Linq-to-SQL?

, Account .., , , , , :

using (MyDataContext dc = _conn.GetContext())
{
    var options = new DataLoadOptions();
    options.LoadWith<Account>(a => a.Email);
    options.LoadWith<Account>(a => a.Profile);
    options.LoadWith<Account>(a => a.HostInfo);

    dc.LoadOptions = options;

    try
    {
        account = (from a in dc.Accounts
                   where a.UserName == userName
                   select a).FirstOrDefault();
    }
    catch
    {
        //oops
    }
}
0

, Email - , . , , . , ctor:

public Account()
{
   // Set Defaults
   Email = new Email();
   Profile = new Profile();
   HostInfo = new HostInfo();
}

.

+3

. - , , , . , , null, .

+2

: :

public Profile Profile { get; set; }
public Email Email { get; set; }
public HostInfo HostInfo { get; set; }
0

Source: https://habr.com/ru/post/1747226/


All Articles