Home > Back-end >  How to search element in LinkedList with custom object
How to search element in LinkedList with custom object

Time:11-06

I need to find a node with input from a customer in the linked list but I have an error CS1503. How can I solve a problem?

In this code, I create the LinkList name "customerList" to collect string data such as name, contact number, and payment from the user. After that, I need to find the contact number which input from the user to show data in the node and delete it. In code show that input in "searchCustomerDetail" cannot convert 'string' to ....

Error Message: Argument 1: cannot convert from 'string' to 'IFN564.Customer' [IFN564]csharp(CS1503)

public class Customer {

    public string Name { get; set; }
    public string Phone { get; set; }
    public string Payment { get; set; }
    public int[] Screening { get; set; }
    public static LinkedList<Customer> customerList = new LinkedList<Customer>();
    public static string input;

    public static void addCustomerDetail() {

        Console.WriteLine("Please enter your information detail");
        Console.WriteLine("");
        Console.Write("Full Name: ");
        string inputName = Console.ReadLine();
        Console.Write("Contact Number: ");
        string inputPhone = Console.ReadLine();
        Console.Write("Payment Method: ");
        string inputPayment = Console.ReadLine();
        Console.Clear();

        Console.WriteLine("");
        Console.WriteLine("Please check your information detail!!");
        Console.WriteLine("");
        Console.WriteLine($"Full Name: {inputName}");
        Console.WriteLine($"Contact Number: {inputPhone}");
        Console.WriteLine($"Payment Method: {inputPayment}");
        Console.WriteLine("");

        Console.WriteLine("Please 1 to confirm or 0 to cancel");
        int input = Convert.ToInt32(Console.ReadLine());

        switch (input) {
            case 1:
                insert(inputName, inputPhone, inputPayment);
            break;
            case 2:
                Program.Main();
            break;
        }
        
    }

    public static void insert(string name, string phone, string payment) {

        Console.WriteLine("");
        Console.WriteLine("Please 1 to confirm buy ticket or 0 to cancel");
        int input = Convert.ToInt32(Console.ReadLine());

        Customer customerDetail = new Customer() {
                Name = name,
                Phone = phone,
                Payment = payment,
        };

        switch (input) {
            case 0: Program.Main(); break;
            case 1: 
                customerList.AddLast(customerDetail); 
                Program.Main();
            break;
        }
 }

    public static void searchCunstomerDetail() {
        Console.WriteLine("Please enter contact number!!");
        Console.WriteLine("");
        Console.Write("Contact number: ");
        input = Console.ReadLine();

        LinkedListNode<Customer> node = customerList.Find(input);
        Console.WriteLine(node);
        
        

    }

}

}

I try to use LinkListNode to find but It show error with input CS1503

CodePudding user response:

you need to use LINQ where

  var node = customerList.Where(c=>c.Phone == input).First();

CodePudding user response:

You should iterate over the linked list. The code below should solve your problem.

public static void searchCustomerDetail() {
        Console.WriteLine("Please enter contact number.\n");
        Console.Write("Contact number: ");
        var input = Console.ReadLine();

        var node = customerList.First;
        while(node != null)
        {
            if (node.Value.Phone == input)
            {
                Console.WriteLine(node);
                break;
            }
            
            node = node.Next;
        }
    }

However, I would maybe use another data structure instead of LinkedList, maybe a List. If you use a List then you can find a match using lambda expression:

var node = customerList.Find(customer => customer.Phone == input);

CodePudding user response:

The error is with this statement:

LinkedListNode<Customer> node = customerList.Find(input);

customerList.Find searches the customerList for an element (in this case, a Customer) that equals the argument provided to Find (in this case, the var, input). input is a string, not Customer, so it can't be used in Find here.

What you seemingly intend to do here is to find the Customer in customerList that has a Customer.Phone that equals the input string. To do so, you can use LINQ, as others have suggested:

LinkedListNode<Customer> node = customerList.FirstOrDefault(c => c.Phone == input);

The above expression will find the first element in the list that returns true for the provided function (predicate), and will return null if none match.

  •  Tags:  
  • c#
  • Related