如何迭代C#中列表中的第一个值和第二个值 [英] How to iterate the first value and the second value in a list in C#

查看:94
本文介绍了如何迭代C#中列表中的第一个值和第二个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

try
    {
        List<int> taskTypeList = new List<int>();

        var flowBranches = from flow in _db.FlowBranches
                .Where(x => x.FlowID == flowID)
                .Select(x => new { x.FirstTaskTypeID, x.SecondTaskTypeID, x.DependencyType }).ToList()
                           select flow;

        foreach (var item in flowBranches)
        {
            var itemQuery = from y in _db.TaskTypes
                            .Where(x => x.TaskTypeId == item.FirstTaskTypeID)
                            .Select(x => x.TaskTypeId).ToList()
                            select y;

            taskTypeList.AddRange(itemQuery);
        }

        foreach (var taskId in taskTypeList)
        {

            //Do stuff
        }

        return 0;
    }





我尝试过:



您好,



我想知道是否有人能让我知道如何迭代第一项列表然后迭代foreach中的第二个元素。



谢谢



What I have tried:

Hello,

I wonder if any of you can give me an idea of ​​how to iterate the first item in a list and then iterate the second element in a foreach.

thanks

推荐答案

它出现 你只想要(一个) foreach 语句来迭代集合的前两个项目。如果那是你的问题,那么只需在语句中对集合使用 .Take(2)方法。例如:

It appears that you just want (one of) the foreach statements to iterate over only the first two items of the collection. If that is your question, then just use the .Take(2) method on the collection in the statement. For example:
foreach (var item in flowBranches.Take(2))


这篇关于如何迭代C#中列表中的第一个值和第二个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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