Home > Blockchain >  How to update FileName from the List in C#?
How to update FileName from the List in C#?

Time:09-14

var test = new List<object>()
        {
            new
            {
                Title = "This is title 1",
                Description = "This is description 1",
                Files = new[] {
                    new {FileName= "File1", FileLocation="FileLoaction1"},
                    new {FileName= "File2", FileLocation="FileLoaction2"},
                    new {FileName= "File3", FileLocation="FileLoaction3"},
                    new {FileName= "File4", FileLocation="FileLoaction4"},
                }
            },
            new
            {
                Title = "This is title 2",
                Description = "This is description 2",
                Files = new[] {
                    new {FileName= "File1", FileLocation="FileLoaction1"},
                    new {FileName= "File2", FileLocation="FileLoaction2"},
                    new {FileName= "File3", FileLocation="FileLoaction3"},
                    new {FileName= "File4", FileLocation="FileLoaction4"},
                }
            }
        };

I am trying to update the value of FilaName for each parent. But I can not, I need help of an expert.

CodePudding user response:

Instead of using new List<object>(), simply use new []. This will allow the compiler to infer the type. In this case it will create an array of the anonymous object you are using.

var test = new []
    {
        new
        {
            Title = "This is title 1",
            Description = "This is description 1",
            Files = new[] {
                new {FileName= "File1", FileLocation="FileLoaction1"},
                new {FileName= "File2", FileLocation="FileLoaction2"},
                new {FileName= "File3", FileLocation="FileLoaction3"},
                new {FileName= "File4", FileLocation="FileLoaction4"},
            }
        },
        new
        {
            Title = "This is title 2",
            Description = "This is description 2",
            Files = new[] {
                new {FileName= "File1", FileLocation="FileLoaction1"},
                new {FileName= "File2", FileLocation="FileLoaction2"},
                new {FileName= "File3", FileLocation="FileLoaction3"},
                new {FileName= "File4", FileLocation="FileLoaction4"},
            }
        }
    };

If you do it this way, you'll find you can access the properties and methods, as long as your code is within the same variable scope.

If you need to cross scope, you need to define a class or struct that has an actual name.

CodePudding user response:

You can't do that with a combination of anonymous types and object because there's no type identifier that you can cast the object type to. You might be able to use reflection, but it would be awfully slow and complicated.

You can use dynamic instead of object, but then you'd lose the compiler safety guarantees.

You can use anonymous type constructors, but they would be harder to maintain IMHO. I wouldn't recommend it.

The best way to approach is to create a class (e.g. Container) for your parents and sub items like:

public class Container {
  public string Title { get; set; }
  public string Description { get; set; }
  public File[] Files { get; set; }
}

public class File {
  public string FileName { get; set; }
  public string FileLocation { get; set; }
}

and modify them to your liking:

foreach (var item in list) {
  foreach (var subItem in item.Files) {
    subItem.FileName = "whatever you want it to be";
  }
}
  • Related