After looking on MSDN, it’s still unclear to me how I should form a proper predicate to use the Find() method in List using a member variable of T (where T is a class)
For example:
public class Name
{
public string FirstName;
public string LastName;
public String Address;
public string Designation;
}
String[] input = new string[] { "VinishGeorge", "PonKumar", "MuthuKumar" };
//ConCatenation of FirstName and Lastname
List<Name> lstName = new List<Name>();
Name objName = new Name();
// Find the first of each Name whose FirstName and LastName will be equal to input(String array declard above).
for(int i =0;i<lstName.Count;i++)
{
objName = lstName .Find(byComparison(x));
Console.Writeline(objName .Address + objName.Designation);
}
What should my byComparison predicate look like?
It’s not clear why you’re looping and calling
Find. Normally you’d callFindnot in a loop – it will loop for you. Anonymous methods are your friends here though:If you’re using C# 3 (even targeting .NET 2) you can use a lambda expression instead:
EDIT: To find all names in
inputyou can use:Note that this won’t be terribly efficient as it will look through the whole of
inputfor a match on everyName. However, the more efficient alternatives are more complicated – I think it’s important for you to understand how this code works to start with.