C# 使用list而不使用foreach子句

C# 使用list而不使用foreach子句,c#,foreach,C#,Foreach,我有一个来自查询表达式的列表,我想使用其中的值,但我不想使用foreach,因为如果它找到正确的值,我不希望它再次循环 var partnerProduct = GetPartnerProducts(); var duesproductP = partnerProduct.ToList(); foreach (var c in duesproductP) { //I wont include all the code in between but there are 3 if clau

我有一个来自查询表达式的列表,我想使用其中的值,但我不想使用foreach,因为如果它找到正确的值,我不希望它再次循环

var partnerProduct = GetPartnerProducts();
var duesproductP = partnerProduct.ToList();
foreach (var c in duesproductP)
{
     //I wont include all the code in between but there are 3 if clauses
}
我不能使用SingleOrDefault,因为有多个值,我不能使用firstordefault,因为它只会给我找到的所有中间子句的第一个值。在我的其他作品中,我有这样的分类标准:

 var duesproductsB = sectionB.Where(o => o.capg_MinCriteria.Value <= dues).ToList().OrderByDescending(o => o.capg_MaxCriteria).FirstOrDefault();
var duesproductsB=sectionB.Where(o=>o.capg_MinCriteria.Value o.capg_MaxCriteria).FirstOrDefault();

但现在我不能,因为没有最小值或最大值,返回的唯一值是价格和Id。它适用于第一个选项和最后一个选项,但不适用于第二个选项。第二个if子句不起作用,因为它不断循环并假定错误的答案。请记住GetPartnerProducts()是一个查询表达式

为什么不简单地检查该值是否是您期望的值,然后跳出循环呢?还是我没有正确理解某些东西?

为什么不简单地检查值是否符合您的预期,然后打破循环?还是我没有正确理解某些事情

但我不想使用foreach,因为如果它找到正确的值, 我不想让它再次循环

var partnerProduct = GetPartnerProducts();
var duesproductP = partnerProduct.ToList();
foreach (var c in duesproductP)
{
     //I wont include all the code in between but there are 3 if clauses
}
如果我理解正确,您希望退出foreach循环,而不完成整个列表的循环

您可以这样做:

 foreach (var c in duesproductP)
            {
                 if(somecondition_met)
                    break; //this will exit the for loop
            }
但我不想使用foreach,因为如果它找到正确的值, 我不想让它再次循环

var partnerProduct = GetPartnerProducts();
var duesproductP = partnerProduct.ToList();
foreach (var c in duesproductP)
{
     //I wont include all the code in between but there are 3 if clauses
}
如果我理解正确,您希望退出foreach循环,而不完成整个列表的循环

您可以这样做:

 foreach (var c in duesproductP)
            {
                 if(somecondition_met)
                    break; //this will exit the for loop
            }

满足条件后,只需中断:

List<bool> conditionsFound = new List<bool> {false,false,false};
foreach (var c in duesproductP)
{
     if(condition1)
     {
         // do something
         conditionsFound[0] = true;
     }
     if(condition2)
     {
         // do something
         conditionsFound[1] = true;
     }
     if(condition3)
     {
         // do something
         conditionsFound[2] = true;
     }

     if(conditionsFound.All())
         break;
}
List conditionsFound=新列表{false,false,false};
foreach(duesproductP中的var c)
{
如果(条件1)
{
//做点什么
conditionsFound[0]=true;
}
如果(条件2)
{
//做点什么
conditionsFound[1]=真;
}
如果(条件3)
{
//做点什么
conditionsFound[2]=真;
}
if(conditionsFound.All())
打破
}

一旦您的条件得到满足,只需
中断即可:

List<bool> conditionsFound = new List<bool> {false,false,false};
foreach (var c in duesproductP)
{
     if(condition1)
     {
         // do something
         conditionsFound[0] = true;
     }
     if(condition2)
     {
         // do something
         conditionsFound[1] = true;
     }
     if(condition3)
     {
         // do something
         conditionsFound[2] = true;
     }

     if(conditionsFound.All())
         break;
}
List conditionsFound=新列表{false,false,false};
foreach(duesproductP中的var c)
{
如果(条件1)
{
//做点什么
conditionsFound[0]=true;
}
如果(条件2)
{
//做点什么
conditionsFound[1]=真;
}
如果(条件3)
{
//做点什么
conditionsFound[2]=真;
}
if(conditionsFound.All())
打破
}

或者如果您还想避免使用break;只要在条件满足时放一个return语句。

或者如果您还想避免使用break;只要在条件满足时放一个返回语句。

请记住,我没有due或min和max值。您是说您有3个条件可以由不同的行满足,并且您希望在所有3个条件都满足时停止?我不清楚您为什么不能编写
.FirstOrDefault(it=>condition1(it)和condition2(it))
这听起来在功能上与
foreach
break
完全相同。请记住,我没有due或min和max值。您是说您有3个不同行可以满足的条件,并且您希望在所有3个条件都满足时停止?我不清楚您为什么不能编写
。FirstOrDefault(it=>条件1(it)和条件2(it))
这听起来在功能上与
foreach
break
完全相同。它在我调试和单步执行时工作,但没有显示正确的值。这将是我需要解决的另一个问题,但谢谢!它在我调试和单步执行时工作,但没有显示正确的值。这将是第二个问题呃,我需要回答的问题,但是谢谢!c#中的return的问题是它将结束条件而不是循环。c#中的return的问题是它将结束条件而不是循环。