I have a file with lines of string. Each line represent a collection of key value, for example:
Name=JUI;Type=HomeUser;Address=Belgium;Address=Liege;Address=Street
Name=Tim;Type=HomeUser;Address=Belgium;Address=Hasselt;Address=Street
Name=Kim;Type=Proff;Address=Germany;Address=Dusseldorf;Address=Street
Name=Ils;Type=Proff;Address=Germany;Address=Munich;Address=Street
Name=Jan;Type=Student;Address=Germany;Address=Frankfurt;Address=Street
Name=Dav;Type=Student;Address=France;Address=Mitz;Address=Street
Name=Soli;Type=HomeUser;Address=France;Address=Lyon;Address=Street
Name=Mik;Type=HomeUser;Address=Switzerland;Address=Zurich;Address=Street
Name=Peter;Type=Blocked;Address=Netherland;Address=Enschede;Address=Street
Name=Maz;Type=Blocked;Address=Germany;Address=Achen;Address=Street
Name=Jo;Type=Teacher;Address=Belgium;Address=Antwerpen;Address=Street
How can I do the following:
- Get the names where type is HomeUser
- Get the types where Address =Germany (problem there are 3 address key in earch line)
- Get the name where address =Lyon
Is there is a simple way to do that?
In all of these cases, the answer is really simple when you’ve got a better data representation – you can just use LINQ.
However, the first step will be to parse the data. Model it something like this:
One you’ve got a
List<User>your queries will be really easy – but it’s important to separate “put data into a more natural representation” from “do interesting operations with data”.This is a much more general point than just this particular example, but always try to get your data into a natural, useful representation as early as you can, and then keep it in that representation for as long as you can. Only deal with an awkward representation (typically a string) at the boundaries of your code, if possible.