linq For循环未写入数组中的所有示例

hfyxw5xn  于 2022-12-06  发布在  其他
关注(0)|答案(3)|浏览(109)

我正在开发一个使用员工列表的程序:

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));

我使用linq进行查询,我想进行顺序搜索,所以我输入:

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");
    
}

这个输出是给我的Songebob 6倍,而不是Songebob和海绵,我做错了什么?我怎么让它正确地迭代?

yb3bgrhw

yb3bgrhw1#

em = match.FirstOrDefault();处,em得到了Songebob。随后,for循环迭代了6次,并将其打印出来。它应该是

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");
}
9rbhqvlz

9rbhqvlz2#

试试这个:

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");
}

希望这对你有帮助。

roejwanj

roejwanj3#

正如@ DotNetDeveloper所说,您的循环在“同一个值”-- Spongebob上迭代了6次。问题是您在For循环中引用了错误的计数。您已经从原始的emp列表中找到了正确雇员“Spongebob”的索引,但是您没有再次引用该值。它们也是结果集中的第一个值,但是您一次又一次地引用同一个雇员,总共引用了6次(emp中的雇员总数),但是您的结果集em只包含2个雇员。
不使用For循环,而是使用@DotNet Developer的建议,对结果集em使用Foreach循环,并在For循环中引用此结果集的计数--x < em.Count;,然后访问列表成员及其列表位置em[x].Name ...

请尝试使用下面的代码来查找匹配项,并在Console.Writeline语句中引用结果集em

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"));
}

相关问题