MongoDB LinQ “Select” method will really retrieve only a subset of fields?

后端 未结 2 404
盖世英雄少女心
盖世英雄少女心 2021-01-06 19:01

Searching across the internet how to retrieve a subset of fields in MongoDB, using C# official driver (but using LinQ as the base architecture) I found how to do this in Mon

2条回答
  •  伪装坚强ぢ
    2021-01-06 19:35

    This is cheating... but:

    //This actual implementation is untested and may contain small errors.
    //The helper method has been tested and *should* work.
    
    public static IMongoQuery GetMongoQuery(this IQueryable query)
    {
        return ((MongoQueryable)query).GetMongoQuery();
    }
    
    var temp =
        from x in DB.Foo.AsQueryable()
        where x.SomeField > 5;
        select (x.OtherField);
    
    return temp.GetMongoQuery().ToJson();
    

提交回复
热议问题