3
foreach (string f in fileName)
{
    if (list.Where(p => p.FileName.Trim().Equals(f.Trim(), StringComparison.OrdinalIgnoreCase)).Count() == 0)
    {
        ServerpathID = GetSourceServerPath(projectID, out ServerPath);
        DellDirectory dir = new DellDirectory(ServerPath);
        lstgAFPFileInfo = GetFilesFromSourceServer(new string[] { f }, ServerpathID, SearchOption.TopDirectoryOnly).ToList();

        if (lstgAFPFileInfo.Count() != 0)
        {
            foreach (Service.GAFPFileInfo lstg in lstgAFPFileInfo)
            {
                projectfile.Checksum = string.Empty;
                projectfile.IsAutoDeleted = (autoDelete == true) ? true : false;
                projectfile.Size = lstgAFPFileInfo[0].Size;
                projectfile.IsArchived = false;
                projectfile.ProjectFileId = 0;
                projectfile.Language.LanguageId = 1;
                projectfile.LastModifyDate = lstgAFPFileInfo[0].LastModified;
                projectfile.ProjectPartLink = projectPartLink;
                projectfile.FileName = f;
                list.Add(projectfile);
            }
        }
    }
}

I have two files 1.txt and 2.txt in string[] filename. I am comparing these files with db and getting values into lstgAFPFileInfo. The first time it got the filename 1.txt and added to list. 2nd time it got the value 2.txt but after adding the file to the list it is overwrite the value 1.txt and again it added 2.txt. Now the list values are like this list[0]:2.txt and list[1]: 2.txt Can anyone help on this?

1
  • As a sidenote, if (lstgAFPFileInfo.Count() != 0) is totally redundant. If lstgAFPFileInfo contains no elements, what would you expect foreach will do? Crash? Why would it. Iterating over an empty sequence does nothing whatsoever. So, this condition only adds noise and unnecessary indentation. Commented Feb 26, 2014 at 13:58

4 Answers 4

7

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.

Sign up to request clarification or add additional context in comments.

Comments

2

You seem to be reusing the same object every time in projectfile. Even after it has been added to the list you have the same object being referenced in the list and in the variable so when you update its properties you update it in both places.

What you need is just to have a line at the beginning of your foreach saying something like:

projectfile = new ProjectFileObject();

This will create a new instance that is totally separate from the one already added to the list.

It should be noted that depending on what you have done with the projectfile object before a more complicated solution may be required but this highlights your basic problem.

Comments

1

Becasue you're adding the same instance each time, just overwriting its properties. You need

projectfile = new WhateverClassNameProjectFileIs();

at the top of your innermost foreach loop.

Comments

0

It looks like you are creating a new string array foreach f in filename.

foreach (string f in fileName)
{
lstgAFPFileInfo = GetFilesFromSourceServer(new string[] { f }, ServerpathID, SearchOption.TopDirectoryOnly).ToList();

Thus, each iteration through the foreach, only the value of f at that time is created in the array. Try instantiating the array outside of your loop and then adding your value f inside.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.