Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have following class, a LinkedList is there has list of Employee object. Now need to search an employee according to name. Like list.Contains(emp.empName) suppose or any thing else.
C#
class Employee
    {
        int empId;
        string empName;
        double basicSal;

        Employee(int eid, string name, double bsal)
        {
            empId = eid;
            empName = name;
            basicSal = bsal;
        }

        static void Main(string[] args)
        {
            LinkedList<Employee> list = new LinkedList<Employee>();
            //List of employees
            list.AddFirst(new Employee(1, "Sam",9000));
            list.AddLast(new Employee(2, "Jeet", 10000));
            list.AddLast(new Employee(3, "John", 11000));



        }

}


Thank You.
Posted
Updated 15-Oct-13 22:41pm
v4

What about LINQ? Something like:


C#
var result = list.Where(emp => emp.empName == "TheName");

Or


C#
var result = list.FirstOrDefault(emp => emp.empName == "TheName");
 
Share this answer
 
I came to one solution.

For the above case i think we cannot use Contins() or Find().
But use ToArray()

C#
Console.Write("Enter a name for search: ");
          string n = Console.ReadLine();

           Employee[] emparr=new Employee[list.Count];
           emparr = list.ToArray();
           int c = 0;
          for (c = 0; c < list.Count; c++)
          {
              if (emparr[c].empName == n)
              {

                  break;
              }

          }

Console.WriteLine("Name is: "+emparr[c].empId+" Basic Salary is: "+emparr[c].basicSal);

Can we use Find() or Contains() method instead ??
 
Share this answer
 
v3
Comments
Shmuel Zang 16-Oct-13 7:37am    
Why don't you use LINQ? (See my solution...)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900