如何在每个循环中使用数组 [英] how to use array in for each loop

查看:112
本文介绍了如何在每个循环中使用数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在桌面上进行查询并获取该表的ID。现在我想获取所有使用该名称的名称,并希望保存在一个数组中我试过一些告诉我这是对的。



I have makea query on table and get the ID of that table.Now i want to get all names using that and want to save in an array i have tried some tell me it is right or not.

Department_PR_LKS objDept = SystemSetupsController.GetDepartmentByDepartmentId(CompanyId);

                    List<Employee_AttandanceLogs_PR_View> objActiveDept = SystemSetupsController.GetAllActiveDepartmentsByID(objDept.DepartmentId);









foreach(objActiveDept中的var项目)

{

数组名称= item.DepartmentName.ToArray();



}





foreach (var item in objActiveDept)
{
Array name= item.DepartmentName.ToArray();

}

推荐答案

试试这个..





Try this..


string[] departmetnNames;
       List<string> lstTemp = new List<string>();
       foreach (var item in objActiveDept)
       {
           lstTemp.Add(item.DepartmentName);
       }
       departmetnNames = lstTemp.ToArray();


目前还不清楚你想要达到什么目标,所以我无法回答这个问题因为这不是一个有效的问题。



我所能提出的建议是:看看你的代码:根本就没有意义。在循环中创建一些数组对象,在进行下一次迭代时将其引用分配给某个局部变量并立即松开此对象。请不要问我该做什么,因为我不知道你想要达到什么目的;只是不要为了任何目的做任何无意义的事情。



-SA
It is quite unclear what do you want to achieve, so I cannot answer this question because this is not a valid question.

All I can advise is: look at your code: it makes no sense at all. You create some array object inside loop, assign its reference to some local variable and immediately loose this object when you go to next iteration. Please don't ask me "what to do instead", because I have no idea what you want to achieve; just don't do what makes no sense for any purpose.

—SA


这篇关于如何在每个循环中使用数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆