1

I have an object defined as

public class EmailData
    {
        public string TemplateId { get; set;}
        public IEnumerable<Recipient> To { get; set; }
        public IEnumerable<Recipient> CC { get; set; }
        public IEnumerable<Recipient> BCC { get; set; }
    }

public class Recipient
{
    public string Key { get; set; }
    public string Type { get; set; }
    public string Email { get; set; }
    public string Id { get; set; }
}

I have that populated in a constructor, then I need to finish populating the Recipient values and in a nutshell I'm going this (after various attempts):

public EmailData EmailObject;

public void BuildEmailData()
{
   EmailDataBuilder builder = new EmailDataBuilder(EmailObject,CSGTemplateId);
   EmailObject = builder.BuildObject();
}

public void PopulateEmailAddresses()
{
   SetToValues();
}

private void SetToValues()
{
   foreach (Recipient t in EmailObject.To)
   {
       var _user = RepoUser.GetUserById(t.Key);
       t.Email = _user.Email;
       t.Id = _user.Id;
   }
}

So you see that I have an object and I need to populate a couple more fields on it. Now, I get that the foreach look creates a variable that I'm using so when I assign the Email value, it's not the the data arg, but rather to the variable. But how do I get around that? Am I missing something simpler with the foreach?

Many thanks, this was bombing my unit tests before I realized that the assignments weren't working.

What I've gotten working that I don't like is to use a collection of Recipients instead of the IEnumerable directly:

public class EmailData
    {
        public string TemplateId { get; set;}
        public RecipientCollection To { get; set; }
        public RecipientCollection CC { get; set; }
        public RecipientCollection BCC { get; set; }
    }

 public class RecipientCollection: IEnumerable<Recipient>
    {
        private readonly List<Recipient> variableList = new List<Recipient>();

        public IEnumerator<Recipient> GetEnumerator()
        {
            return variableList.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        public void Add(Recipient v)
        {
            variableList.Add(v);
        }
    }

 public EmailData BuildObject()
        {
            // get the template.
            EmailObj.TemplateId = _templateRepo.GetCSGEmailTemplate(CSGTemplateId).TemplateId;

            // populate the people
            RecipientCollection _toCollection = new RecipientCollection();
            RecipientCollection _ccCollection = new RecipientCollection();
            RecipientCollection _bccCollection = new RecipientCollection();

            foreach (var r in _recipRepo.GetRecipientByAction("To"))
            { _toCollection.Add(r); }
            EmailObj.To = _toCollection;

            foreach (var r in _recipRepo.GetRecipientByAction("CC"))
            { _ccCollection.Add(r); }
            EmailObj.CC = _ccCollection;

            foreach (var r in _recipRepo.GetRecipientByAction("BCC"))
            { _bccCollection.Add(r); }
            EmailObj.BCC = _bccCollection;

            return EmailObj;
        }

 public void BuildEmailData()
        {
            EmailDataBuilder builder = new EmailDataBuilder(EmailObject,CSGTemplateId);
            EmailObject = builder.BuildObject();
        }

public void PopulateEmailAddresses()
        {
            foreach (Recipient t in EmailObject.To)
            {
               var _user = RepoUser.GetUserById(t.Key);
               t.Email = _user.Email;
               t.Id = _user.Id;
             }
        }

It's not ideal as is breaks another piece to this which uses Linq to XML but I can figure that out.

2
  • Honestly I'd like to do this via Linq but I can't seem to find a way to assign values as such. Commented Oct 15, 2010 at 16:51
  • Hm ... sometimes you need a plain old for loop with indices instead of foreach, but I do not have a great answer. Also, what are the actual types? Could you temporarily stop using var? What type do you have to use then? stackoverflow.com/questions/2196638/… Commented Oct 15, 2010 at 16:57

2 Answers 2

1

Is Recipient a Struct? Change it to a class perhaps.

Sign up to request clarification or add additional context in comments.

Comments

1

Since .To has set, do:

  • create a new list of recipients
  • foreach original recipient construct new and insert it into the new list
  • assign new list to .To

That should work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.