Questo snippet vuole condividere una porzione di codice di esempio per i metodi Find e FindAll su Liste di oggetti c#.
Supponiamo di avere la seguente classe Automobile:

/**//// <summary>
/// Classe Automobile
/// </summary>
public class Automobile

...{
private String colore;
private String marca;
//Costruttore
public Automobile(string _marca, string _colore)

...{
this.marca = _marca;
this.colore = _colore;
}

public String Colore ...{

get ...{ return this.colore; }

set ...{ this.colore = value; }
}
public String Marca

...{

get ...{ return this.marca; }

set ...{ this.marca = value; }
}
}
e di voler cercare in una Lista di Automobili un oggetto di un certo colore, avremo:
List<Automobile> myList = new List<Automobile>();
Automobile auto = null;
auto = new Automobile("FIAT", "rosso");
myList.Add(auto); //Add to List
auto = new Automobile("BMW", "blu");
myList.Add(auto); //Add to List
Automobile foundObj = myList.Find(delegate(Automobile a) { return a.Colore == "rosso"; });
//Restituisce FIAT
Response.Write(foundObj.Marca);
Lo snippet precedente restituisce la prima occorrenza nell'intera classe List, quindi la Response.Write stampa a video "FIAT".
Se, invece, vogliamo che la Find resituisca tutte le occorrenze nella lista, dobbiamo utilizare la FindAll come di seguito:
List<Automobile> myList = new List<Automobile>();
Automobile auto = null;
auto = new Automobile("FIAT", "rosso");
myList.Add(auto); //Add to List
auto = new Automobile("BMW", "blu");
myList.Add(auto); //Add to List
List<Automobile> foundList = null;
foundList = myList.FindAll(
delegate(Automobile objFind)
{
return ((objFind.Colore == "rosso")
&&
(objFind.Marca == "FIAT"));
});
Response.Write(foundList.Count.ToString());
In questo caso la Response.Write stampaerà a video il numero 1.
Enjoy snippet