简体   繁体   中英

datastructure for parsing a file in C#

I have a file in the following format consisting of multiple entries in different lines. DirectorName MovieName GrossRevenue . eg

abc movie1 1mill
xyz movie2 2mill
abc movie3 1mill
abc movie2 4mill

the director name repeats itself and so does the moviename (director1 can direct a movie with the same name as director2)? what would be the best way to parse the file while providing the ability to get details like, total revenue for a director. I was planning on having a Dictionary of dictionaries something like

   Dictionary<string, Dictionary<string,long>> 

with the key for the outer dictionary being director name and the key for the inner dictionary being movie name. I could make this look better by wrapping my inner dictionary within a custom class but I was wondering if there is a better approach to this problem. Also how would i design this so that if later my file has additional content like genre, actors etc added , i would still not have to do a complete redesign while providing the ability to group by genre or actor

Rather than using the Dictionary, You can use a custom class with Generic List.

you can create a class like

public class Director {

 public string StrDirector { get; set; }
 public string StrMovie { get; set; }
 public Long Revenue { get; set; }

}

then use the

List<Director> LstEntity = new List<Director>();
Director objDirector = new Director();
objDirector.StrDirector = "abc";
objDirector.StrMovie = "xyz";
objDirector.Revenue = 1000;
LstEntity.Add(objDirector);

//Add each Director Same way

This way, In future , if you need to make any modification in the director's attributes, you can easily make by updating the class without any problem.

I hope that will resolve your problem.

I would first add a class

public class Movie
{
    public string MovieName {get;set;}
    public DateTime ProductionDate {get;set}
    public decimal Revenue  {get;set;}

    public List<MoviePerson> Stuff {get;set;}
}

public class MoviePerson
{
   public enum PersonType{Actor,Director, ....}

   public string Name {get;set;}
   public string Surname {get;set;}
   public PersonType Duty {get;set}
   ...
}

Having the collection of Movies you can simply query it for director, example

List<Movie> movies = new List<Movie>();
....

MoviePerson director = 
         new MoviePerson {Name = "James",
                                Surname = "Cameron", 
                                   PersonType = Director}
from movie in movies where movie.Stuff.Contains(director) select movie

To make this work you have to override default reference comparison of MoviePerson .

These are just ideas, you have to fit them to your needs.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM