C# 带有通用对象列表的 List.Contains(item)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/503734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 05:54:39  来源:igfitidea点击:

List.Contains(item) with generic list of objects

c#

提问by Nic

If you have a List how do you return the item if a specified property or collection of properties exists?

如果您有一个列表,如果存在指定的属性或属性集合,您如何返回该项目?

public class Testing
{
    public string value1 { get; set; }
    public string value2 { get; set; }
    public int value3 { get; set; }
}
public class TestingList
{
    public void TestingNewList()
    {
        var testList = new List<Testing>
                           {
                               new Testing {value1 = "Value1 - 1", value2 = "Value2 - 1", value3 = 3},
                               new Testing {value1 = "Value1 - 2", value2 = "Value2 - 2", value3 = 2},
                               new Testing {value1 = "Value1 - 3", value2 = "Value2 - 3", value3 = 3},
                               new Testing {value1 = "Value1 - 4", value2 = "Value2 - 4", value3 = 4},
                               new Testing {value1 = "Value1 - 5", value2 = "Value2 - 5", value3 = 5},
                               new Testing {value1 = "Value1 - 6", value2 = "Value2 - 6", value3 = 6},
                               new Testing {value1 = "Value1 - 7", value2 = "Value2 - 7", value3 = 7}
                           };

        //use testList.Contains to see if value3 = 3
        //use testList.Contains to see if value3 = 2 and value1 = "Value1 - 2"


    }
}

采纳答案by CubanX

If you're using .NET 3.5 or better, LINQ is the answer to this one:

如果您使用的是 .NET 3.5 或更高版本,LINQ 就是解决这个问题的答案:

testList.Where(t => t.value3 == 3);
testList.Where(t => t.value3 == 2 && t.value1 == "Value1 - 2");

If not using .NET 3.5 then you can just loop through and pick out the ones you want.

如果不使用 .NET 3.5,那么您可以循环并挑选出您想要的。

回答by Alfa

Look at the Findor FindAllmethod of the List<T>class.

查看类的FindorFindAll方法List<T>

回答by Mark

You could use

你可以用

testList.Exists(x=>x.value3 == 3)

回答by bdukes

If you want to use the class's implementation of equality, you can use the Containsmethod. Depending on how you define equality (by default it'll be referential, which won't be any help), you may be able to run one of those tests. You could also create multiple IEqualityComparer<T>s for each test you want to perform.

如果要使用类的相等实现,可以使用该Contains方法。根据您定义相等性的方式(默认情况下它将是参考性的,这不会有任何帮助),您可能能够运行这些测试之一。您还可以为IEqualityComparer<T>要执行的每个测试创建多个s。

Alternatively, for tests that don't rely just on the class's equality, you can use the Existsmethod and pass in a delegate to test against (or Findif you want a reference to the matching instance).

或者,对于不仅仅依赖于类的相等性的测试,您可以使用该Exists方法并传入一个委托进行测试(或者Find如果您想要对匹配实例的引用)。

For example, you could define equality in the Testingclass like so:

例如,您可以Testing像这样在类中定义相等性:

public class Testing: IEquatable<Testing>
{
    // getters, setters, other code
    ...

    public bool Equals(Testing other)
    {
        return other != null && other.value3 == this.value3;
    }
}

Then you would test if the list contains an item with value3 == 3 with this code:

然后,您将使用以下代码测试列表是否包含 value3 == 3 的项目:

Testing testValue = new Testing();
testValue.value3 = 3;
return testList.Contains(testValue);

To use Exists, you could do the following (first with delegate, second with lambda):

要使用 Exists,您可以执行以下操作(首先使用委托,其次使用 lambda):

return testList.Exists(delegate(testValue) { return testValue.value3 == 3 });

return testList.Exists(testValue => testValue.value3 == 2 && testValue.value1 == "Value1 - 2");

回答by Jim Petkus

A LINQ query would probably be the easiest way to code this.

LINQ 查询可能是对此进行编码的最简单方法。

Testing result = (from t in testList where t.value3 == 3 select t).FirstOrDefault();