Convert a string to a list

Crystal Rouse
Crystal Rouse used Ask the Experts™
on
I have the following code that returns one record. I need to convert this to a list and return single or more than one.

public string GetApproverEmail()
        {
            try
            {

                var user =  (from x in DB.tbl_User
                        where x.Approver == true
                        select x).Single();

                var email = (from x in DB.tbl_GlobalUser
                                  where x.ID == user.UserID
                                  select x.Email).Single();

                return (email);

            }

            catch
            {
                return null;
            }
        }

Open in new window


This sends the email:

public static void Approve(tbl_ChangeRequest cr)
        {
            User user = new User();
            Email mail = new Email("Approval");
            string Email = user.GetApproverEmail();

            string body = ("This is an automated email.<br />" + "" +
                "text here"" +
                 " is ready for approval. <br />" + "<br />" +
             );

            mail.SetBody(body, true);
            mail.AddTo(Email);
            mail.AddCC("");

            mail.Send();
        }

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
So, do you want to send a single email to multiple recipients, or multiple emails, one to each recipient?


»bp
Crystal RouseTask Lead

Author

Commented:
A single email to multiple recipients.
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
Okay, I think this should get you pretty close, although you might have to shake it out a little there to perfect it.

public List<string> GetApproverEmail()
   {
       try
       {

           var user =  (from x in DB.tbl_User
                   where x.Approver == true
                   select x).Single();

           List<string> email = (from x in DB.tbl_GlobalUser
                             where x.ID == user.UserID
                             select x.Email).ToList();

           return (email);

       }

       catch
       {
           return null;
       }
   }



public static void Approve(tbl_ChangeRequest cr)
   {
       User user = new User();
       Email mail = new Email("Approval");
       List<string> email = user.GetApproverEmail();

       string body = ("This is an automated email.<br />" + "" +
           "text here"" +
            " is ready for approval. <br />" + "<br />" +
        );

       mail.SetBody(body, true);
       foreach (string e in email) {
           mail.AddTo(e);
       }
       mail.AddCC("");

       mail.Send();
   }

Open in new window


»bp
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

Crystal RouseTask Lead

Author

Commented:
Thank you so much!  For the user:

var user =  (from x in DB.tbl_User
                   where x.Approver == true
                   select x).Single();


I actually need to return a list since there will be more than one Approver. So, I think I need to return the List of emails for all users.
I probably need to add a semi-colon after each one as well.  Can you help me update this?  This is part I have problems with is figuring
out lists and how to loop thru them.
Test your restores, not your backups...
Top Expert 2016
Commented:
It actually sounds like you want to do a join between the two tables in the user ID, and get all the email addresses for all users that are approvers.  I think this change should do that and as before returns a List object of the email addresses that you can then use to build the TO: address of the outgoing email.

public List<string> GetApproverEmail()
   {
       try
       {

           List<string> email = (from u in DB.tbl_User
                                 join gu in DB.tbl_GlobalUser on gu.Id equals u.UserId
                                 where u.Approver == true
                                 select gu.Email).ToList();

           return (email);

       }

       catch
       {
           return null;
       }
   }



public static void Approve(tbl_ChangeRequest cr)
   {
       User user = new User();
       Email mail = new Email("Approval");
       List<string> email = user.GetApproverEmail();

       string body = ("This is an automated email.<br />" + "" +
           "text here"" +
            " is ready for approval. <br />" + "<br />" +
        );

       mail.SetBody(body, true);
       foreach (string e in email) {
           mail.AddTo(e);
       }
       mail.AddCC("");

       mail.Send();
   }

Open in new window


»bp
Crystal RouseTask Lead

Author

Commented:
Thank You!  Now I forgot about joining tables.  I really appreciate it!
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
Welcome, glad that was helpful.


»bp

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial