How can I find a specific item in a List <T>?
My application uses the following list:
List<MyClass> list = new List<MyClass>();
Using the Add method, another instance of MyClass added to the list.
MyClass provides, among other things, the following methods:
public void SetId(String Id); public String GetId(); How can I find a specific instance of MyClass using the GetId method? I know there is a Find method, but I donβt know if this will work here ?!
Use lambda expression
MyClass result = list.Find(x => x.GetId() == "xy"); Note. C # has built-in property syntax. Instead of writing Set- and Get-methods write
private string _id; public string Id { get { return _id; } set { _id = value; } } value is a contextual keyword known only in the accessor set. It represents the value assigned to the property.
Because this pattern is often used, C # provides automatically implemented properties. This is the short version of the code above; however, the supporting variable is hidden and inaccessible (however, it is accessible from a class in VB).
public string Id { get; set; } You can simply use the properties as if you were accessing a field:
var obj = new MyClass(); obj.Id = "xy"; // Calls the setter with "xy" assigned to the value parameter. string id = obj.Id; // Calls the getter. Using properties, you should look for items in a list like this
MyClass result = list.Find(x => x.Id == "xy"); You can also use automatically implemented properties if you need a read-only property:
public string Id { get; private set; } This allows you to set the Id inside the class, but not externally. If you also need to set it in derived classes, you can also protect the setter
public string Id { get; protected set; } And finally, you can declare properties as virtual and redefine them when deriving classes, allowing you to provide various implementations for getters and setters; as with conventional virtual methods.
Since C # 6.0 (Visual Studio 2015, Roslyn) you can write auto-properties only for getter with a built-in initializer
public string Id { get; } = "A07"; // Evaluated once when object is initialized. You can also initialize getter-only properties inside the constructor. Getter-only auto properties are read-only true properties, unlike auto-implemented properties using a private setter.
This also works with automatic read and write properties:
public string Id { get; set; } = "A07"; Starting in C # 6.0, you can also write properties as elements with a body expression.
public DateTime Yesterday => DateTime.Date.AddDays(-1); // Evaluated at each call. // Instead of public DateTime Yesterday { get { return DateTime.Date.AddDays(-1); } } See: .NET Compiler Platform (Roslyn)
New language features in C # 6
Starting with C # 7.0 , both getter and setter can be written using expression bodies:
public string Name { get => _name; // getter set => _name = value; // setter } Please note that in this case, the installer must be an expression. This cannot be a statement. The above example works because in C #, assignment can be used as an expression or as an operator. The value of an assignment expression is the assigned value. The appointment itself is a side effect. This allows you to immediately assign the value to several variables: x = y = z = 0 equivalent to x = (y = (z = 0)) .
var list = new List<MyClass>(); var item = list.Find( x => x.GetId() == "TARGET_ID" ); or if there is only one and you want something like SingleOrDefault to be what you want
var item = list.SingleOrDefault( x => x.GetId() == "TARGET" ); if ( item == null ) throw new Exception(); Try:
list.Find(item => item.id==myid); You can also use LINQ extensions :
string id = "hello"; MyClass result = list.Where(m => m.GetId() == id).First(); You can most accurately solve your problem with a predicate written using anonymous method syntax:
MyClass found = list.Find(item => item.GetID() == ID); Or, if you do not prefer to use LINQ , you can do this in the old school:
List<MyClass> list = new List<MyClass>(); foreach (MyClass element in list) { if (element.GetId() == "heres_where_you_put_what_you_are_looking_for") { break; // If you only want to find the first instance a break here would be best for your application } }