regarding C# adding objects to list using foreach loop

前端 未结 4 2112
遇见更好的自我
遇见更好的自我 2021-01-06 17:06
foreach (string f in fileName)
{
    if (list.Where(p => p.FileName.Trim().Equals(f.Trim(), StringComparison.OrdinalIgnoreCase)).Count() == 0)
    {
        Serve         


        
4条回答
  •  离开以前
    2021-01-06 17:38

    This is because your loop keeps adding the same object over and over, so your list ends up with multiple references to the same object.

    Illustration

    Add projectfile = new ProjectFile() to the top of your loop to fix this problem.

提交回复
热议问题