【问题标题】:List.Contains(item) with generic list of objectsList.Contains(item) 与对象的通用列表
【发布时间】:2009-02-03 05:36:51
【问题描述】:

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

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"


    }
}

【问题讨论】:

    标签: c#


    【解决方案1】:

    你可以使用

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

    【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

    • 既然已经为列表定义了 Find、FindAll 和 Exists,为什么还要导入 LINQ?
    • @bdukes Necro 评论回复,只是之前没有注意到 :) Find 在 3.5 之前的版本中使用对象相等运算符来确定相等。您可以提供自己的,但在 3.5 之前,这很痛苦。但是从 3.5 开始,他们添加了 lambda 版本,您可以在其中根据需要定义相等性。我说 LINQ 是因为它是我想到的第一个符合他的要求的东西,但我认为你是对的,如果你已经有一个 List,你应该使用内置的运算符。如果他有其他没有的东西,LINQ 就可以满足要求。
    • 是的,还有其他方法。但是 +1 提醒我可以在我的代码中使用:SiteList.Where(s =&gt; s.GLCode == glcode);。 :-)
    【解决方案3】:

    查看List&lt;T&gt; 类的FindFindAll 方法。

    【讨论】:

      【解决方案4】:

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

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

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

      public class Testing: IEquatable<Testing>
      {
          // getters, setters, other code
          ...
      
          public bool Equals(Testing other)
          {
              return other != null && other.value3 == this.value3;
          }
      }
      

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

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

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

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

      【讨论】:

        【解决方案5】:

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

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

        【讨论】:

        • 在 C# 中,您需要在查询表达式中使用 select 子句。在这种情况下,IMO 直接使用 Where 方法会更容易。
        • 糟糕。添加了选择。 vb.net 和 c# 之间的弹跳太多。
        猜你喜欢
        • 2018-10-07
        • 1970-01-01
        • 2020-07-08
        • 1970-01-01
        • 2011-06-07
        • 2021-12-06
        • 1970-01-01
        • 1970-01-01
        • 2021-10-21
        相关资源
        最近更新 更多