Home > Software design >  Search data in C# from Database SQL Server
Search data in C# from Database SQL Server

Time:02-08

I want to ask you regarding for the searching in C# and why there is a Not Found statement if already found the answer or the data itself.

Here is the code:

                    Console.WriteLine("Enter Plate Number: ");
                    string plateNumber1 = Console.ReadLine();
                    var searchPlateNumberDAL = new ParkingSystemDAL(_iconfiguration);
                    var listSlotParking = searchPlateNumberDAL.GetList();
                    listSlotParking.ForEach(item =>
                    {
                        bool searchItem = item.plateNumber == plateNumber1;
                        if (searchItem == true)
                        {
                            Console.WriteLine(item.parkingId);
                        }
                        else
                        {
                            Console.WriteLine("Not Found");
                        }
                    });

output

CodePudding user response:

I think there some garbage value is present.

So, code will be

if(item.plateNumber.ToString().Trim().ToUpper() == plateNumber1.ToString().Trim().ToUpper())
{
  Console.WriteLine(item.parkingId);
}

you can also use Linq Where expression instead of ForEach

if( listSlotParking.Where(x =>x.planteNumberplateNumber.ToString().Trim().ToUpper() == plateNumber1.ToString().Trim().ToUpper()).ToList().Count()>0)
{
 Console.WriteLine(item.parkingId);
}

CodePudding user response:

Try

item.plateNumber.Trim() == plateNumber1.Trim()

  •  Tags:  
  • Related