迭代LINQ,索引在集合中
本文关键字:集合 索引 LINQ 迭代 | 更新日期: 2023-09-27 18:07:32
private void Right_Click(object sender, EventArgs e)
{
using (var ctx = new NORTHWNDEntities())
{
int txBoxId = int.Parse(Id.Text);
var query = (from x in ctx.Employees where x.Employees1.Count >= txBoxId + 1 select x);
List<Employee> emp = query.ToList();
foreach (Employee c in emp)
{
Id.Text = c.EmployeeID.ToString();
FirstName.Text = c.FirstName;
LastName.Text = c.LastName;
DateOfBirth.Text = c.BirthDate.Value.ToShortDateString();
}
}
}
我想通过索引来收集,有什么建议吗?
emp
是List
,您可以简单地遍历正常for
循环中的每个元素:
for(int index = 0; index < emp.Count; index++)
{
var employee = emp[index];
//...
}
刚刚看到你的编辑,你可以在你的查询中使用方法语法而不是查询语法来访问索引LINQ:
var query = ctx.Employees
.Where((x, index) => x.Employees1.Count >= txBoxId + 1);