14

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"


    }
}
Nic
  • 732
  • 3
  • 11
  • 24

5 Answers5

26

You could use

testList.Exists(x=>x.value3 == 3)
bdukes
  • 152,002
  • 23
  • 148
  • 175
Mark
  • 9,966
  • 7
  • 37
  • 39
23

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

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.

bdukes
  • 152,002
  • 23
  • 148
  • 175
CubanX
  • 5,176
  • 2
  • 29
  • 44
  • Why bother importing LINQ when Find, FindAll and Exists are already defined for the list? – bdukes Feb 02 '09 at 19:17
  • @bdukes Necro comment response, just didn't notice it before :) Find in versions before 3.5 used the objects equality operator to determine equality. You could supply your own, but before 3.5 it was a pain. But since 3.5 they added the lambda version where you can define equality as necessary. I said LINQ because it was the first thing I thought of that matched his request, but I think you're right, if you already have a List, you should use the built in operators. If he has something else that doesn't have those, LINQ would fit the bill. – CubanX Aug 17 '09 at 14:51
  • Yeah, there are other ways. But +1 for reminding me that I can use: `SiteList.Where(s => s.GLCode == glcode);` in my code. :-) – cbmeeks Apr 12 '12 at 13:22
17

Look at the Find or FindAll method of the List<T> class.

bdukes
  • 152,002
  • 23
  • 148
  • 175
Alfa
  • 201
  • 2
  • 6
8

If you want to use the class's implementation of equality, you can use the Contains method. 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.

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

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

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:

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):

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

return testList.Exists(testValue => testValue.value3 == 2 && testValue.value1 == "Value1 - 2");
bdukes
  • 152,002
  • 23
  • 148
  • 175
1

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

Testing result = (from t in testList where t.value3 == 3 select t).FirstOrDefault();
bdukes
  • 152,002
  • 23
  • 148
  • 175
Jim Petkus
  • 4,500
  • 25
  • 19