csharp LINQ FindAll

public static void Integers()
{
	//create List
	List<int> intList = new List<int>();

	//add 100 numbers to the list
	for (int i = 0; i < 100; i++)
	{
		intList.Add(i);
	}

	//2 predicate examples, intList2 will contain all those that are true
	List<int> intList2 = intList.FindAll(Evens);
	//List<int> intList2 = intList.FindAll(Odds);

	//alternatively: using Lambda to find Odds
	//List<int> intList2 = (from r in intList.Where(num => num % 2 != 0) select r).ToList();

	//loop through and display
	foreach (var item in intList2)
	{
		Console.WriteLine(item);
	}

	Console.Read();
}

public static bool Evens(int value)
{
	//remainder 0 = even number
	if(value % 2 == 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

public static bool Odds(int value)
{
	//remainder = odd number
	if (value % 2 != 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}
Example of LINQ's FindAll using a predicate and also the same using a Lambda expression.

Updated: Tuesday 5th October 2010, 09:58am

There are 0 comments

Leave a comment of your own

Comments are currently closed.