Deserialize an IEnumerable<BsonDocument> into an IEnumerable<NominalType>

161 views
Skip to first unread message

Alex Brown

unread,
Jul 9, 2012, 8:22:43 AM7/9/12
to mongodb...@googlegroups.com
Is it possible to use the BsonSerializer to Deserialize an IEnumerable<BsonDocument> into an IEnumerable<NominalType>?

Currently it seems I have to do something nasty like:

var result = new List<Discipline>();

            foreach (var item in bsonDocs)
                 result  .Add(BsonSerializer.Deserialize<MyNominalType>(item));

            return results;

Robert Stam

unread,
Jul 9, 2012, 9:16:18 AM7/9/12
to mongodb...@googlegroups.com
See response to the same question in the mongodb-user group.

Alex Brown

unread,
Jul 9, 2012, 9:18:45 AM7/9/12
to mongodb...@googlegroups.com
Thanks..
Had deleted that post due to this group (mongodb-csharp) being the right place for it...
not sure if posts can be moved across groups?

anyway, thanks again

On 9 July 2012 14:16, Robert Stam <rob...@10gen.com> wrote:
mongodb-user

Robert Stam

unread,
Jul 9, 2012, 9:23:07 AM7/9/12
to mongodb...@googlegroups.com
I didn't realize you had deleted the post. I responded by email, which may have resurrected the post.

In any case, here's what I replied in mongodb-user:

===== original reply in mongodb-user =====
BsonSerialize.Deserialize is designed to deserialize a single document at a time.

However, you could write your conversion from IEnumerable<BsonDocument> to IEnumerable<NominalType> on a single line of code:

    return bsonDocs.Select(b => BsonSerializer.Deserialize<MyNominalType>(b));

which is essentially doing the same thing but using the LINQ Select method.
=====
Reply all
Reply to author
Forward
0 new messages