{"id":1835,"date":"2013-01-26T16:56:00","date_gmt":"2013-01-26T22:56:00","guid":{"rendered":"http:\/\/www.wiredprairie.us\/blog\/?p=1835"},"modified":"2013-01-26T14:39:01","modified_gmt":"2013-01-26T20:39:01","slug":"how-to-rewrite-a-mongodb-c-linq-with-a-projection-requirement-using-a-mongocursor","status":"publish","type":"post","link":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/1835","title":{"rendered":"How to rewrite a MongoDB C# LINQ with a Projection Requirement using a MongoCursor"},"content":{"rendered":"

The LINQ Provider for MongoDB does not currently<\/a> take into account data projections efficiently when returning data. This could mean that you\u2019re unnecessarily returning more data from the database than is needed. <\/p>\n

So, I\u2019m going to show you the pattern I applied as a replacement for the LINQ queries when I need to use a projection.<\/p>\n

Given the following simple LINQ statement:<\/p>\n

var query = \n    (from r in<\/span> DataLayer.Database\n         .GetCollection<Research>()\n         .AsQueryable<Research>()\n        where<\/span> !r.Deleted\n        select new<\/span>\n        {\n            Id = r.Id,\n            Title = r.Title,\n            Created = r.Created\n        }).Skip(PageSize * page).Take(PageSize);<\/pre>\n