2017-01-27 41 views
1

我有一個包含兩個字段的文檔; namedocuments使用C#SDK讀取(反序列化)時忽略字段

我希望這兩個字段在寫入期間被序列化並插入到我的集合中。但是,我不想documents讀取時反序列化(我很高興與這一領域被空),

下面是實體類:

public class EmployeeDocument 
{ 
    [BsonElement("name")] 
    public string Name { get; set; } 

    [BsonElement("documents")] 
    public List<string> Documents { get; set; } 
} 

我怎麼能做到這一點?

回答

1

我認爲不可能忽略反序列化的屬性,但不能用於序列化。 我認爲這不是最好的方法。 如果你想閱讀文檔並不是所有的屬性,您可以排除他們預測,如果你不是想擁有同樣的EmployeeDocument類,你可以反序列化BsonDocument它,我的意思是這樣:

var emps = db.GetCollection<EmployeeDocument>("empl"); 
var employees =emps.Find(x=>true) 
      .Project(Builders<EmployeeDocument>.Projection.Exclude(x=>x.Documents)) 
      .ToList() 
      .Select(x=>BsonSerializer.Deserialize<EmployeeDocument>(x));