在lambda表达式中转换嵌套for循环

时间:2014-04-18 04:33:56

标签: c# linq lambda

我是编程新手。任何人都可以告诉我如何在lambda表达式中转换这个for循环

for (int num = 0; num < daysNew.Count(); num++)
{
    for (int count = 0; count < daysOld.Count(); count++)
    {
        if (daysOld[count] == daysNew[num])
        {
            isValid = false;
            break;
        }
    }
}

2 个答案:

答案 0 :(得分:1)

这应该这样做:

bool isValid = !daysNew.Any(day => daysOld.Contains(day));

答案 1 :(得分:0)

使用List.Any()方法循环:

bool isValid = !daysNew.Any(day => daysOld.Contains(day));

bool isValid = !daysOld.Any(day => daysNew.Contains(day));

它将为“daysNew”中每个元素的每个元素检入“daysOld”