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