Tuesday, July 23, 2013

CSharp C# Tuple Linq search get IEnumerable from List

CSharp C# Tuple Linq search get IEnumerable from List

meta: predicate

Output:


joe_Now.Item1 = Joe joe_Now.Item2 = Joe_First
joe_Now.Item1 = Joe joe_Now.Item2 = Joe_Second

press enter to finish


Program.cs:

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleStuff
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Tuple<string, string>> tupleCollectionOptionalClasses = new List<Tuple<string, string>>();
            tupleCollectionOptionalClasses.Add(new Tuple<string, string>("Hank","Hank_First"));
            tupleCollectionOptionalClasses.Add(new Tuple<string, string>("Joe", "Joe_First"));
            tupleCollectionOptionalClasses.Add(new Tuple<string, string>("Mary", "Mary_First"));
            tupleCollectionOptionalClasses.Add(new Tuple<string, string>("Joe", "Joe_Second"));
            tupleCollectionOptionalClasses.Add(new Tuple<string, string>("Hank", "Hank_Second"));

            IEnumerable<Tuple<string, string>> tuple_JustJoe = tupleCollectionOptionalClasses.Where(
                  delegate(Tuple<string, string> mapNowFinder)
                  {
                      return mapNowFinder.Item1 == "Joe"// case sensitive
                  }
                  );

            foreach (Tuple<string, string> joe_Now in tuple_JustJoe)
            {
                Console.WriteLine("joe_Now.Item1 = {0} joe_Now.Item2 = {1}", joe_Now.Item1, joe_Now.Item2);
            }

            Console.WriteLine();
            Console.WriteLine("press enter to finish");
            Console.ReadLine();

        }
    }
}



CSharp C# example string split array to List object

CSharp C# example string split array to List object

This is a quick example of using the string  Split function directly to a System.Collections.Generic.List object and a quick Linq search

Key line:

List<string> listOfStuff = new List<string>(myCommaSeperatedListOfStuff.Split(new char[] { ',' }));


Output:

apple

press enter to finish


Program.cs:

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleStuff
{
    class Program
    {
        static void Main(string[] args)
        {

            string myCommaSeperatedListOfStuff = "one,two,apple,shoe";           
            List<string> listOfStuff = new List<string>(myCommaSeperatedListOfStuff.Split(new char[] { ',' }));
            Console.WriteLine(listOfStuff.First((findit) => findit == "apple"));
            Console.WriteLine();
            Console.WriteLine("press enter to finish");
            Console.ReadLine();

        }
    }
}



Monday, July 22, 2013

CSharp C# delegate predicate to find Tuple in a List




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;
                   }
               );
       }
   }
}