简体   繁体   中英

Search or Find IO Serialized objects in file with c#

I am serializing c# objects and appent to file. But so many objects increasing in file. About 20000 object.

   public void SerializeObject(string filename, ObjectToSerialize objectToSerialize)
   {
      Stream stream = File.Open(filename, FileMode.Create);
      BinaryFormatter bFormatter = new BinaryFormatter();
      bFormatter.Serialize(stream, objectToSerialize);
      stream.Close();
   }

And later I want to get an object from this file. So how can I find it. Is deserializing 20000 object in an array and search by linq make sense? Or is there any practical way?

Programming language not important may be c++ or c#.

Without any other metadata, deserializing the object is your only option. You won't be able to query the object until you deserialize it, and yes deserializing 20,000 objects will be hard.

But, since YOU serialized it in the first place, YOU could persist some meta data that would make it easier to search in the first place.

For example:

Dictionary<string,string> Index = new Dictionary<string,string>();
Index.Add(fileIdentifierThatYouWouldSearchOn,fileName);

Then you could serialize the Index. Everytime you persist a new file, also maintain the Index file. Then you can deserialize just the Index, find the filename you want, and load up just that file.

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