Search code examples
c#stringlinqsplittestcase

How to test split char test to search?


I'm trying to create select search to to find people by first name or last name but when the user type search for name I have couple of cases

  1. fullName String ex"john"
  2. fullName contains split char "." ex:john.dow
  3. fullName contains split char "_" ex:john_dow
  4. fullName contains split char " " ex:john dow

My problem is if always run the first case my Question how can I make my code run the right case

C#

using System;

namespace separatefullnamestring
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            string query = "fulname.kkkkk";


//          var result  = from tableA in ContextDB.tblA
//              join tableB in ContextDB.tblb tableA. ID equals tableB.ID
//              select tableA;
//

            //String.isNullOrEmpty(query)
            if (!String.IsNullOrEmpty (query)) {
                Console.WriteLine ("fullname ");
                //result = result.where(p => p.FirstName.containe(query) || p.LastName.containe(query));

            } else // if full name contine containe . firstName.LastName
                if (query.Contains (".")) {
                Console.WriteLine ("Contains .");
//                  var names = fullName.Split ('.');
//                  string fName = names[0];
//                  string lName = names[1];
//                  result = result.where(p => p.FirstName.containe(fName) || p.LastName.containe(lName));

            } else   // if full name contine containe _ firstName_LastName
                    if (query.Contains ("_")) {
                Console.WriteLine ("Contains .");
//                      var names = fullName.Split ('_');
//                      string fName = names[0];
//                      string lName = names[1];
//                      result = result.where(p => p.FirstName.containe(fName) || p.LastName.containe(lName));

            } else   // if full name contine containe space firstName_LastName
                        if (query.Contains (" ")) {
                Console.WriteLine ("Contains ");
                //var names = fullName.Split ('_');
                //string fName = names[0];
                //string lName = names[1];
                //result = result.where(p => p.FirstName.containe(fName) || p.LastName.containe(lName));

            }



        }
    }
}

Solution

  • There were a few issues with the code.

    1. The first conditional "!String.IsNullOrEmpty (query)" would force any non-null or empty query value to stop processing at this time. Solution: Increased the scope of the conditional, so if the case is true, the application would end.

    2. The conditional statement for "else if (query.Contains ("_"))" Console.Writeline displays the exact same text as the conditional above it, thus making debugging a little difficult because you were unsure which statement you were on. Solution: Corrected the text in the Writeline statement.

    Updated code:

    string query = "fulname_kkkkk";
    
    // var result  = from tableA in ContextDB.tblA
    //              join tableB in ContextDB.tblb tableA. ID equals tableB.ID
    //              select tableA;
    //
    //String.isNullOrEmpty(query)
    if (!String.IsNullOrEmpty (query)) {
        if (query.Contains (".")) {
            Console.WriteLine ("Contains .");
    //                  var names = fullName.Split ('.');
    //                  string fName = names[0];
    //                  string lName = names[1];
    //                  result = result.where(p => p.FirstName.containe(fName) || p.LastName.containe(lName));
    
        } else if (query.Contains ("_")) {
            Console.WriteLine ("Contains _");
    //                      var names = fullName.Split ('_');
    //                      string fName = names[0];
    //                      string lName = names[1];
    //                      result = result.where(p => p.FirstName.containe(fName) || p.LastName.containe(lName));
        } else if (query.Contains (" ")) {
            Console.WriteLine ("Contains ");
                    //var names = fullName.Split ('_');
                    //string fName = names[0];
                    //string lName = names[1];
                    //result = result.where(p => p.FirstName.containe(fName) || p.LastName.containe(lName));
        } else {
            Console.WriteLine ("fullname ");
            //result = result.where(p => p.FirstName.containe(query) || p.LastName.containe(query));
        }
        } else {
            Console.WriteLine("Exiting program");
        }