我需要四个变量的FOREACH循环或for循环的代码
【腾讯云】亏本大甩卖,服务器4核16G 1年370元(带宽12M,系统盘120GB SSD盘,月流量2000GB)!!!!!!
云产品 配置 价格
服务器 1核2G,带宽5M,系统盘50GB SSD盘,月流量500GB 38元/年
MySQL 1核1G 19元/年
服务器 16核32G,带宽18M,系统盘250GB SSD盘,月流量5000GB 1197元/年
点我进入腾讯云,查看更多详情

I have four arrays of equal length I am using those arrays in foreach loops. I am using four variables(i,j,k,l) to increment and to proceed

I am writing my code inside the four nested loops, that code should execute when i=0,j=0,k=,0,l=0 i=1,j=1,k=1,l=1 i=2,j=2,k=2,l=2 .....(depending on the array length)

Please suggest me the code for this required segment.

int i = 0, j = 0, k = 0, l = 0;
          foreach (string fieldName in splitFieldnames)
          {
              i = 0;
              foreach (string dataType in splitDatatypeNames)
              {
                  j = 0;
                  foreach (string controlName in SplitControlNames)
                  {
                      k = 0;
                      foreach (string controlType in splitControlTypeNames)
                      {
                          if (i == j && j == k && k == l)
                          {

                              if (controlType == "textbox" && dataType == "string")
                              {

                                  Response.Write("_Student." + fieldName + "= " + controlName + ".Text;");
                                  l++;
                                  break;
                              }

                          }
                          k++;

                      }

                      j++;
                  }



                  i++;

              }

          }

#0

I think using LINQ would save you a lot of manual coding:

static void Main(string[] args)
{
    var splitFieldnames = new string[] { "field1", "field2", "field3" };
    var splitDatatypeNames = new string[] { "datatype1", "string", "string" };
    var SplitControlNames = new string[] { "control1", "control2", "control3" };
    var splitControlTypeNames = new string[] { "combobox", "textbox", "textbox"};

    // this code can handle different sized arrays, but is based strictly
    // on the size of the splitFieldnames array as the base.
    var splitMerged = splitFieldnames.Select
        ((c, idx) =>
            new
            {
                fieldName = c,
                dataType = splitDatatypeNames.Length > idx ? 
                    splitDatatypeNames[idx] : "",
                controlName = SplitControlNames.Length > idx ? 
                    SplitControlNames[idx] : "",
                controlTypeName = splitControlTypeNames.Length > idx?
                    splitControlTypeNames[idx] : "",
            });

   foreach (var item in splitMerged
      .Where(c => c.controlTypeName == "textbox" && c.dataType == "string"))
   {
       Response.Write("_Student." + item.fieldName + "= " 
           + item.controlName + ".Text;");
   }

The resulting output would be look like:

_Student.field2= control2.Text;
_Student.field3= control3.Text;

I hope that's what you're looking for, LOL...

#1

I think that in this case a DataTable would be better than 4 arrays, and algorithm you require will be trivial with such data structure.

#2

If what you are trying to accomplish is process the same index in the four arrays, just use one loop and use a counter to access the value in each array in that one loop:

foreach(string fieldName in SplitControlNames)
{
    dataType = splitDatatypeNames[arrayPosition];
    controlName = SplitControlNames[arrayPosition];
    controlType  = splitControlTypeNames[arrayPosition];

    if (controlType == "textbox" && dataType == "string")
        Response.Write("_Student." + fieldName + "= " + controlName + ".Text;");

    arrayPosition++;
}

Or, create and populate a structure that has four values in it (fieldName, dataType, controlName, controlType) and have one array of that structure

#3

Ok, this code is really simple.. If you only need to show the data when i=j=k=l, then no need for any loop, or even for the existence of i,j,k,l. If you can assure me that all this strings in whatever thing (a collection? an array? a dictionary?) are ordered, is ok. If they are ordered, then throw away all the for each, and just access each one by position for all the controls you have in the collection that has the minimun. If all this collection are not ordered, then this is totally uselles, since it will produce differents results on each run. I will edit this answers with some code, once you can tell me if this is ordered or not.

EDIT:

First of all, you need to check what collection has the least items (since you can't go beyond that)... I don't know the types of this things (you didn't provide them), so let's assume they have a count property.

int minimun = splitFieldnames.count;
if (splitDatatypeNames.count < minimun)
    minimun = splitDatatypeNames.count;
if (SplitControlNames.count < minimun)
    minimun = SplitControlNames.count
if (splitControlTypeNames.count < minimun)
    minimun = splitControlTypeNames.count

once you have the minimun value (since you can't go beyond that), just iterate on that and print whatever you want

for (int i = 0; i < minimun;i++)
{
    if (splitControlTypeNames[i].tostring() == "textbox" && splitDatatypeNames[i].tostring() == "string")
        {
            //Response.Write("_Student." + fieldName + "= " + controlName + ".Text;");
            //Also, a parametric string would be better ;)
            string result = string.format("_Student.{0}= {1}.Text;",splitFieldnames[0].tostring(),SplitControlNames[0].tostring());
            Response.Write(result);
            l++;
        }
}

I don't know the types, so I'm assuming that they have a tostring method an a count property

#4

Use for instead of foreach

for (int i = 0; i < splitFieldnames.Length; i++)
{
    string fieldName = splitFieldnames[i];
    for (int j = 0; j < splitDatatypeNames.Length; j++)
    { 
        string dataType = splitDatatypeNames[j];
        for (int k = 0; k < SplitControlNames.Length; k++)
        { 
            string controlName = SplitControlNames[k];
            for (int l = 0; l < splitControlTypeNames.Length; l++)
            {
                string controlType = splitControlTypeNames[l];
                if (i == j && j == k && k == l)
                { 
                    if (controlType == "textbox" && dataType == "string")
                    { 
                        Response.Write("_Student." + fieldName + "= " + controlName + ".Text;");
                        break;
                    }
                }
            }
        }
    }
}

Note also that break will only break out of the innermost loop! Consider using a return statement instead.


UPDATE (in response to your edit):

The solution is simple, use only one index variable and only one for-loop instead of a lot of foreach-loops

for (int i = 0; i < splitFieldnames.Length; i++)
{
    if (splitControlTypeNames[i] == "textbox" && splitDatatypeNames[i] == "string")
    { 
        Response.Write("_Student." + splitFieldnames[i] + "= " + SplitControlNames[i] + ".Text;");
        break;
    }
}

(Assuming that you want to stop after the first match. If you want to output all string textboxes, drop the break statement.)

推荐文章

两个几乎完全相同的实体,自定义连接注释

两个几乎完全相同的实体,自定义连接注释

推荐文章

Linq2Sql自定义映射

Linq2Sql自定义映射

推荐文章

我需要通过一个字符串进行解析并提取信息以输入到数据库中

我需要通过一个字符串进行解析并提取信息以输入到数据库中

推荐文章

交互式列表视图

交互式列表视图

推荐文章

寻找特定的所见即所得编辑器功能。建议?

寻找特定的所见即所得编辑器功能。建议?

推荐文章

jQuery冲突(?)添加和删除li元素时

jQuery冲突(?)添加和删除li元素时

推荐文章

外部创建返回已实例化的类

外部创建返回已实例化的类

推荐文章

如何用多个Tkinter复选按钮对齐列?

如何用多个Tkinter复选按钮对齐列?

推荐文章

UISplitviewController和UINavigationController

UISplitviewController和UINavigationController

推荐文章

OAuth:尝试获取LinkedIn的访问令牌时的HTTP 401状态

OAuth:尝试获取LinkedIn的访问令牌时的HTTP 401状态

推荐文章

jQuery公司表单.提交()无响应脚本错误

jQuery公司表单.提交()无响应脚本错误

推荐文章

查找并替换字符串及其后面的内容

查找并替换字符串及其后面的内容

推荐文章

在上发布java代码wordpress.com网站. 突出显示问题和丢失的缩进

在上发布java代码wordpress.com网站. 突出显示问题和丢失的缩进

推荐文章

Android仿真器在本地Zend服务器上的测试

Android仿真器在本地Zend服务器上的测试

推荐文章

如何实现C事件处理?

如何实现C事件处理?

推荐文章

RowCommand上的Gridview单元格值

RowCommand上的Gridview单元格值