0

How can I process the foreach loop in MVC. In the bellow controller I am calling a method called SendSimpleMessage() which sends an email to its parameter input but on my controller I am unable to use foreach properly.

screenshot

[HttpPost]
        public ActionResult EmailCampaignProcess(FormCollection collection)
        {
            //var userType = Request["userType"];
            //var emailContent = Request["emailContent"];
            //SendSimpleMessage();
            //ViewBag.Message = "Hello " + Request["userType"];


            var Emails = db.Users.Where(d => d.Subscriptions.Any(x => x.Status == true)).Select(u => u.Email).ToArray();


            foreach (Emails as Email) {

                SendSimpleMessage(Email);

            }

        }
3
  • foreach (email in Emails) { SendSimpleMessage(email); } you iterate trough Emails, not Email. Then for each "email" you do something. Commented Jun 25, 2017 at 12:22
  • Recommended reading dotnetperls.com/foreach Commented Jun 25, 2017 at 12:34
  • You forgot to ask a question. Questions in English go with a question mark, ?, and can receive an answer. Please ask an actual question, as that's what you filled out a box for. Commented Jun 25, 2017 at 20:49

2 Answers 2

1

Your code is wrong, a foreach loop should look like

foreach (var currentEmail in Emails) { //where var can be your Class maybe Email
    SendSimpleMessage(currentEmail);
}

Generally a foreach looks like:

foreach(T objectName in YourCollection){
       //T is your class
       //objectName is the way you access the object within your loop
       //in references the list
       //YourCollection is IEnumerable<T>
}
Sign up to request clarification or add additional context in comments.

Comments

0

Defintion of ForEach Statement

The for each statement is used to iterate through a collection. You can modify elements in a collection, but you cannot add or delete elements.The statements are executed for each element in the array or collection. After the iteration has been completed for all the elements in the collection, control is transferred to the statement that follows the for each block

Syntax:

   for each (type identifier in expression) 
  {  
       statements  
    }  

Parameters type

The type of identifier.

identifier

The iteration variable that represents the collection element. When identifier is a Tracking Reference Operator, you can modify the element.

expression

An array expression or collection. The collection element must be such that the compiler can convert it to the identifier type.

statements

One or more statements to be executed.

Simple Example:

string[] countries = { "india", "US", "UK" };


        foreach (string value in countries )
        {
            Console.WriteLine(value);
        }

In the same way your code will change like below:

 [HttpPost]
            public ActionResult EmailCampaignProcess(FormCollection collection)
            {
                //var userType = Request["userType"];
                //var emailContent = Request["emailContent"];
                //SendSimpleMessage();
                //ViewBag.Message = "Hello " + Request["userType"];


                var Emails = db.Users.Where(d => d.Subscriptions.Any(x => x.Status == true)).Select(u => u.Email).ToArray();

 foreach (string SingleEmail in Emails) {

                    SendSimpleMessage(SingleEmail);

                }
         // Or if you are not sure about the outcome type you can use the var keyword like below

                foreach (var SingleEmail in Emails) {

                    SendSimpleMessage(SingleEmail);

                }

            }

Hope the above information was helpful

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.