Convert a string to a list

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

Crystal RouseTask LeadAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewIT / Software Engineering ConsultantCommented:
So, do you want to send a single email to multiple recipients, or multiple emails, one to each recipient?


»bp
Crystal RouseTask LeadAuthor Commented:
A single email to multiple recipients.
Bill PrewIT / Software Engineering ConsultantCommented:
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
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Crystal RouseTask LeadAuthor 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.
Bill PrewIT / Software Engineering ConsultantCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Crystal RouseTask LeadAuthor Commented:
Thank You!  Now I forgot about joining tables.  I really appreciate it!
Bill PrewIT / Software Engineering ConsultantCommented:
Welcome, glad that was helpful.


»bp
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET MVC

From novice to tech pro — start learning today.