C# 如何在我正在迭代的列表中找到一个对象

C# 如何在我正在迭代的列表中找到一个对象,c#,generics,lambda,C#,Generics,Lambda,我正在遍历类型为“prveEmployeeIncident”的对象列表 该对象具有以下属性: public DateTime DateOfIncident { get; set; } public bool IsCountedAsAPoint; public decimal OriginalPointValue; public bool IsFirstInCollection { get; set; } public bool IsLastInCollection { get; set; }

我正在遍历类型为“prveEmployeeIncident”的对象列表

该对象具有以下属性:

public DateTime DateOfIncident { get; set; }
public bool IsCountedAsAPoint; 
public decimal OriginalPointValue;
public bool IsFirstInCollection { get; set; }
public bool IsLastInCollection { get; set; }
public int PositionInCollection { get; set; }
public int DaysUntilNextPoint { get; set; }
public DateTime DateDroppedBySystem { get; set; }
public bool IsGoodBehaviorObject { get; set; }
我的列表按DateOfIncident属性排序。我想找到下一个对象upIsCounted==true的列表,并将其更改为IsCounted=false

一个问题:


1) 如何在列表中查找此对象?

如果我理解正确,可以通过简单的foreach循环解决此问题。我不完全理解你对“向上”的强调,因为你不是真的向上移动列表,而是遍历它。无论如何,下面的代码片段会找到第一个事件,其中IsCounted为true,并将其更改为false。如果从给定位置开始,请将for each循环更改为for循环,并从
i=currentIndex
开始,退出条件为
i
。保留break语句以确保只修改一个事件对象

  foreach (prvEmployeeIncident inc in MyList)
  {
       if (inc.IsCountedAsAPoint)
       {
          inc.IsCountedAsAPoint = false;
          break;
       }
  }

如果我理解正确,这可以通过一个简单的foreach循环来解决。我不完全理解你对“向上”的强调,因为你不是真的向上移动列表,而是遍历它。无论如何,下面的代码片段会找到第一个事件,其中IsCounted为true,并将其更改为false。如果从给定位置开始,请将for each循环更改为for循环,并从
i=currentIndex
开始,退出条件为
i
。保留break语句以确保只修改一个事件对象

  foreach (prvEmployeeIncident inc in MyList)
  {
       if (inc.IsCountedAsAPoint)
       {
          inc.IsCountedAsAPoint = false;
          break;
       }
  }

如果我正确理解您的问题,您可以使用LINQ:


如果我正确理解您的问题,您可以使用LINQ:

您可以使用搜索列表

例如:

public class Foo
{
    public Foo() { }

    public Foo(int item)
    {
        Item = item;
    }

    public int Item { get; set; }
}

var foos = new List<Foo>
                {
                    new Foo(1),
                    new Foo(2),
                    new Foo(3),
                    new Foo(4),
                    new Foo(5),
                    new Foo(6)
                };

foreach (var foo in foos)
{
    if(foo.Item == 3)
    {
        var startIndex = foos.IndexOf(foo) + 1;
        var matchedFooIndex = foos.FindIndex(startIndex, f => f.Item % 3 == 0);
        if(matchedFooIndex >= startIndex) // Make sure we found a match
            foos[matchedFooIndex].Item = 10;
    }
}
公共类Foo
{
公共Foo(){}
公共食品(国际项目)
{
项目=项目;
}
公共int项{get;set;}
}
var foos=新列表
{
新富(1),,
新富(2),,
新富(3),,
新富(4),,
新富(5),,
新富(6)
};
foreach(foos中的var foo)
{
如果(foo.Item==3)
{
var startIndex=foos.IndexOf(foo)+1;
var matchedFooIndex=foos.FindIndex(startIndex,f=>f.项%3==0);
if(matchedFooIndex>=startIndex)//确保找到匹配项
foos[matchedFooIndex]。项=10;
}
}

请确保不要修改列表本身,因为这将引发异常。

您可以使用来搜索列表

例如:

public class Foo
{
    public Foo() { }

    public Foo(int item)
    {
        Item = item;
    }

    public int Item { get; set; }
}

var foos = new List<Foo>
                {
                    new Foo(1),
                    new Foo(2),
                    new Foo(3),
                    new Foo(4),
                    new Foo(5),
                    new Foo(6)
                };

foreach (var foo in foos)
{
    if(foo.Item == 3)
    {
        var startIndex = foos.IndexOf(foo) + 1;
        var matchedFooIndex = foos.FindIndex(startIndex, f => f.Item % 3 == 0);
        if(matchedFooIndex >= startIndex) // Make sure we found a match
            foos[matchedFooIndex].Item = 10;
    }
}
公共类Foo
{
公共Foo(){}
公共食品(国际项目)
{
项目=项目;
}
公共int项{get;set;}
}
var foos=新列表
{
新富(1),,
新富(2),,
新富(3),,
新富(4),,
新富(5),,
新富(6)
};
foreach(foos中的var foo)
{
如果(foo.Item==3)
{
var startIndex=foos.IndexOf(foo)+1;
var matchedFooIndex=foos.FindIndex(startIndex,f=>f.项%3==0);
if(matchedFooIndex>=startIndex)//确保找到匹配项
foos[matchedFooIndex]。项=10;
}
}


请确保您不修改列表本身,因为这将引发异常。

可能是下一个匹配的项。可能是下一个匹配项。是否可以在已经迭代的集合中使用lambda?@ USER 107312:不应该是因为它不是更好的性能,但是,你能把你的代码发布更多的理解吗?我可以在一个我已经在迭代的中间的集合上使用lambda吗?@ USER 107312:它不应该是因为它不是更好的性能,但是你能把你的代码张贴以获得更多的理解吗?