C# 在MVC中使用Foreach循环

C# 在MVC中使用Foreach循环,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,如何在MVC中处理foreach循环。在下面的控制器中,我调用了一个名为SendSimpleMessage()的方法,该方法向其参数输入发送电子邮件,但在我的控制器上,我无法正确使用foreach 您的代码是错误的,foreach循环应该如下所示 foreach (var currentEmail in Emails) { //where var can be your Class maybe Email SendSimpleMessage(currentEmail); } 通常,f

如何在MVC中处理
foreach
循环。在下面的控制器中,我调用了一个名为
SendSimpleMessage()
的方法,该方法向其参数输入发送电子邮件,但在我的控制器上,我无法正确使用
foreach


您的代码是错误的,
foreach
循环应该如下所示

foreach (var currentEmail in Emails) { //where var can be your Class maybe Email
    SendSimpleMessage(currentEmail);
}
通常,foreach看起来像:

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>
}
foreach(集合中的T objectName){
//这是你的班级
//objectName是在循环中访问对象的方式
//列表中的参考文献
//你的收藏是数不清的
}

ForEach语句的定义

for-each语句用于遍历集合。您可以修改集合中的元素,但不能添加或删除元素。这些语句将针对数组或集合中的每个元素执行。集合中所有元素的迭代完成后,控制权将转移到每个块的

语法:

   for each (type identifier in expression) 
  {  
       statements  
    }  
string[] countries = { "india", "US", "UK" };


        foreach (string value in countries )
        {
            Console.WriteLine(value);
        }
 [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);

                }

            }
参数类型

标识符的类型

标识符

表示集合元素的迭代变量。当标识符是跟踪引用运算符时,可以修改元素

表达

数组表达式或集合。集合元素必须是编译器可以将其转换为标识符类型的元素

声明

要执行的一个或多个语句

简单示例:

   for each (type identifier in expression) 
  {  
       statements  
    }  
string[] countries = { "india", "US", "UK" };


        foreach (string value in countries )
        {
            Console.WriteLine(value);
        }
 [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);

                }

            }
以同样的方式更改代码,如下所示:

   for each (type identifier in expression) 
  {  
       statements  
    }  
string[] countries = { "india", "US", "UK" };


        foreach (string value in countries )
        {
            Console.WriteLine(value);
        }
 [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);

                }

            }

希望以上信息对您有所帮助

foreach(电子邮件中的电子邮件){SendSimpleMessage(电子邮件);}您可以通过电子邮件而不是电子邮件进行迭代。然后,对于每封“电子邮件”,你都会做一些事情。推荐阅读时,你忘了问一个问题。英语中的问题带有问号,?,可以得到答案。请问一个实际的问题,因为这是您填写的方框。