Home > other >  For Loop not writing all instances in array
For Loop not writing all instances in array

Time:10-09

i am working on a program that uses a list of employees:

List<Employee> emp = new List<Employee>();
   emp.Add(new Employee("Bran", 2500));
   emp.Add(new Employee("Mike", 1500));
   emp.Add(new Employee("Isaac", 1000));
   emp.Add(new Employee("Patrick", 100));
   emp.Add(new Employee("Spongebob", 2000));
   emp.Add(new Employee("Spongegar", 1200));

I am using linq to make a query and i want to do a sequential search so i put:

Console.WriteLine("\nResults of sequential search ");
Employee em = null; //flag 
string name = "Spo";
int i;

var match = from e in emp
            where e.Name.StartsWith(name)
            select e; 

em = match.FirstOrDefault();
i = emp.IndexOf(em);


MoreEmployee();
if (match.Any())
{
    for (int x =0; x < emp.Count; x  )
    {
        Console.WriteLine((x  1)   ". "   em.Name   "    "   em.Salary.ToString("C")   "    "   em.AnnualSal.ToString("C"));
        
    }
    
}
else
{
    Console.WriteLine("Employee Not found");
    
}

this output is giving me Songebob 6 times and not Songebob and Spongegar, what im i doing wrong? how do i get it to iterate through properly?

CodePudding user response:

At em = match.FirstOrDefault(); the em is getting Songebob. And your for loop later on is iterating 6 times, printing it. It should be

if (match.Any())
{
    var x = 1;
    foreach (var empl in match)
    {
        Console.WriteLine(x     ". "   empl.Name   "    "
              empl.Salary.ToString("C")   "    "   empl.AnnualSal.ToString("C"));
    }
}
else
{
    Console.WriteLine("Employee Not found");
}

CodePudding user response:

try this:

List<Employee> emp = new List<Employee>();
emp.Add(new Employee("Bran", 2500));
emp.Add(new Employee("Mike", 1500));
emp.Add(new Employee("Isaac", 1000));
emp.Add(new Employee("Patrick", 100));
emp.Add(new Employee("Spongebob", 2000));
emp.Add(new Employee("Spongegar", 1200));

string name = "Spo";

var match= emp.Where(y => y.Name.StartsWith(name)).ToList();

if (match.Any())
{
     for (int x = 0; x < match.Count; x  )
     {
         Console.WriteLine((x  1)   ". "   match[x].Name   "    "   
         match[x].Salary.ToString("C")   "    "  match[x].AnnualSal.ToString("C"));
      }
}
else
{
    Console.WriteLine("Employee Not found");
}

hope this helps...

CodePudding user response:

As @DotNet Developer said, your loop is iterating 6 times "on the same value" -- Spongebob. The problem is that you are referencing the wrong count in your For loop. You have found the index of the correct employee "Spongebob" from your original emp list, but you don't reference that value again. They are also the first in the result set, but you are referencing that same employee over and over again a total of 6 times (the number of total employees in emp) but your result set, em only contains 2 employees.

Instead of using a For loop, either use the suggestion by @DotNet Developer and use a Foreach loop on the result set, em and refer to the count of this result set in your For loop -- x < em.Count; then access the list memebers with their list position em[x].Name ...
OR
Try this instead of your code to find the match and reference the result set em in your Console.Writeline statement:

Console.WriteLine("\nResults of sequential search ");
string name = "Spo";
List<Employee> em = emp.Where(e => e.Name.StartsWith(name)).ToList();

for (int x = 0; x < em.Count; x  )
{
    Console.WriteLine((x   1)   ". "   em[x].Name   "    "   em[x].Salary.ToString("C")   "    "   em[x].AnnualSal.ToString("C"));
}


/* or simplify accessing the list elements by using a Foreach loop */
Console.WriteLine("or, using a Foreach loop:");

int i = 0;
foreach (var employee in em)
{
    Console.WriteLine((  i)   ". "   employee.Name   "    "   employee.Salary.ToString("C")   "    "   employee.AnnualSal.ToString("C"));
}
  • Related