This example shows how to use a predicate to search for a Tuple (record) object.
This example uses a List of type Tuple.
Output:
Program.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleStuff
{
class Program
{
static void Main(string[] args)
{
List<Tuple<string, string, string>> DirsToUnmap = new List<Tuple<string, string, string>>();
{
Tuple<string, string, string> mapNow = new Tuple<string, string, string>("Alpha", "Beta", null);
DirsToUnmap.Add(mapNow);
Console.WriteLine("Just added the following Tuple to the List of Tuples:");
ShowTupleContent(mapNow);
}
{
Tuple<string, string, string> mapNow = new Tuple<string, string, string>("Alpha", "George", null);
DirsToUnmap.Add(mapNow);
Console.WriteLine("Just added the following Tuple to the List of Tuples:");
ShowTupleContent(mapNow);
}
Console.WriteLine("DirsToUnmap count = {0}", DirsToUnmap.Count);
{
Console.WriteLine("Checking to see if Tuple exists...");
Tuple<string, string, string> mapNow = new Tuple<string, string, string>("Alpha", "George", null);
ShowTupleContent(mapNow);
Console.WriteLine("Does the above Tuple exist in the List? Ans: {0}", CheckIfTupleExists(DirsToUnmap, mapNow));
}
{
Console.WriteLine("Checking to see if Tuple exists...");
Tuple<string, string, string> mapNow = new Tuple<string, string, string>("Alpha", "Kevin", null);
ShowTupleContent(mapNow);
Console.WriteLine("Does the above Tuple exist in the List? Ans: {0}", CheckIfTupleExists(DirsToUnmap, mapNow));
}
Console.ReadLine();
}
private static void ShowTupleContent(Tuple<string, string, string> tNow)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Tuple values now = {0}, {1}, {2}", tNow.Item1, tNow.Item2, tNow.Item3);
Console.ResetColor();
}
private static bool CheckIfTupleExists(List<Tuple<string, string, string>> DirsToUnmap, Tuple<string, string, string> mapNow )
{
return DirsToUnmap.Exists(
delegate(Tuple<string, string, string> mapNowFinder)
{
return mapNowFinder.Item1 == mapNow.Item1 && mapNowFinder.Item2 == mapNow.Item2 &&
mapNowFinder.Item3 == mapNow.Item3;
}
);
}
}
}
|
No comments:
Post a Comment