C# 包含(项)和对象的通用列表

C# 包含(项)和对象的通用列表,c#,C#,如果您有一个列表,如果存在指定的属性或属性集合,如何返回该项 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 te

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

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"


    }
}
公共类测试
{
公共字符串值1{get;set;}
公共字符串值2{get;set;}
公共int值3{get;set;}
}
公共类测试列表
{
public void TestingNewList()
{
var testList=新列表
{
新测试{value1=“value1-1”,value2=“value2-1”,value3=3},
新测试{value1=“value1-2”,value2=“value2-2”,value3=2},
新测试{value1=“value1-3”,value2=“value2-3”,value3=3},
新测试{value1=“value1-4”,value2=“value2-4”,value3=4},
新测试{value1=“value1-5”,value2=“value2-5”,value3=5},
新测试{value1=“value1-6”,value2=“value2-6”,value3=6},
新测试{value1=“value1-7”,value2=“value2-7”,value3=7}
};
//使用testList.Contains查看value3是否为3
//使用testList.Contains查看value3=2和value1=“value1-2”
}
}

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

testList.Where(t => t.value3 == 3);
testList.Where(t => t.value3 == 2 && t.value1 == "Value1 - 2");
如果不使用.NET 3.5,则可以循环使用并选择所需的。

查看类的or方法。

您可以使用

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

如果要使用类的相等实现,可以使用该方法。根据您定义相等的方式(默认情况下它是引用的,这没有任何帮助),您可能可以运行其中一个测试。您还可以为要执行的每个测试创建多个

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

例如,您可以在
测试
类中定义相等,如下所示:

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

    public bool Equals(Testing other)
    {
        return other != null && other.value3 == this.value3;
    }
}
要使用Exists,可以执行以下操作(首先使用delegate,其次使用lambda):


LINQ查询可能是编写此代码的最简单方法

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

在C#中,查询表达式中需要一个select子句。在本例中,直接使用Where方法更容易,IMO.Oops。添加了选择。vb.net和c#之间的跳转太多。既然已经为列表定义了Find、FindAll和Exists,为什么还要导入LINQ?@bdukes Necro comment response,只是以前没有注意到:)3.5之前的版本中的Find使用了objects equality操作符来确定相等性。你可以自己供应,但在3.5之前,这是一种痛苦。但从3.5版开始,他们添加了lambda版本,在该版本中,您可以根据需要定义相等。我说LINQ是因为这是我想到的第一件符合他的要求的事情,但我认为你是对的,如果你已经有了一个列表,你应该使用内置操作符。如果他有其他没有的东西,LINQ会符合条件的。是的,还有其他的方法。但是+1提醒我可以使用:
SiteList.Where(s=>s.GLCode==GLCode)在我的代码中。:-)+1再次强调可用性。阿尔索
return testList.Exists(delegate(testValue) { return testValue.value3 == 3 });

return testList.Exists(testValue => testValue.value3 == 2 && testValue.value1 == "Value1 - 2");
Testing result = (from t in testList where t.value3 == 3 select t).FirstOrDefault();