{"id":95,"date":"2010-05-09T22:06:00","date_gmt":"2010-05-10T04:06:00","guid":{"rendered":"https:\/\/www.chrisedwards.dreamhosters.com\/blog\/?p=95"},"modified":"2010-06-14T23:07:25","modified_gmt":"2010-06-15T05:07:25","slug":"using-linq-with-mongodb","status":"publish","type":"post","link":"http:\/\/architester.com\/blog\/2010\/05\/09\/using-linq-with-mongodb\/","title":{"rendered":"Using Linq with MongoDB"},"content":{"rendered":"<p><a href=\"https:\/\/www.chrisedwards.dreamhosters.com\/blog\/2010\/05\/02\/accessing-mongodb-via-c\/\" target=\"_blank\">Last week<\/a> we looked at accessing <a href=\"http:\/\/www.mongodb.org\/\" target=\"_blank\">MongoDB<\/a> using the C# driver. This week we will look at accessing it using Linq.<\/p>\n<p>If you followed the instructions for last weeks post, you already have everything you need to get up and running. So lets just jump right in.<\/p>\n<h1>Referencing MongoDB.Linq<\/h1>\n<p>To access the Linq functionality of the MongoDB C# driver, you need to add a reference to the MongoDB.Linq.dll which comes in the distribution with the C# driver. Simply add a reference to it.<\/p>\n<p><a href=\"https:\/\/www.chrisedwards.dreamhosters.com\/blog\/wp-content\/uploads\/2010\/05\/image2.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border-width: 0px;\" title=\"image\" src=\"https:\/\/www.chrisedwards.dreamhosters.com\/blog\/wp-content\/uploads\/2010\/05\/image_thumb2.png\" border=\"0\" alt=\"image\" width=\"244\" height=\"237\" \/><\/a><\/p>\n<h1>Write the Linq Code<\/h1>\n<p>Next we want to replace the query code from last week with the Linq query code. So find the following code in last week\u00e2\u20ac\u2122s Program.cs file.<\/p>\n<pre class=\"csharp\">\/\/ Create a specification to query the orders collection.\r\nvar spec = new Document();\r\nspec[\"customerName\"] = \"Elmer Fudd\";\r\n\r\n\/\/ Run the query.\r\nDocument result = orders.FindOne( spec );<\/pre>\n<p>And replace it with this code\u00e2\u20ac\u00a6<\/p>\n<pre class=\"csharp\">\/\/ Query the orders collection.\r\nvar results =\r\n\tfrom doc in orders.AsQueryable()\r\n\twhere doc.Key( \"customerName\" ) == \"Elmer Fudd\"\r\n\tselect doc;\r\nvar result = results.FirstOrDefault();<\/pre>\n<p>The <span style=\"font-family: 'Courier New';\">IMongoCollection<\/span> interface exposes the <span style=\"font-family: 'Courier New';\">AsQueryable()<\/span> method which gives you access to all the Linq goodness.<\/p>\n<p>I realize this was a very short post this week, but that is more a testament to the ease of use than to the laziness of the blogger.<\/p>\n<p>Next week we\u00e2\u20ac\u2122ll dig deeper into querying the db.<\/p>\n<p>-Chris<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Last week we looked at accessing MongoDB using the C# driver. This week we will look at accessing it using Linq. If you followed the instructions for last weeks post, you already have everything you need to get up and running. So lets just jump right in. Referencing MongoDB.Linq To access the Linq functionality of [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27,31,43,24,23],"tags":[79,30,90,81,80],"class_list":["post-95","post","type-post","status-publish","format-standard","hentry","category-net","category-c","category-linq","category-mongodb","category-nosql","tag-net","tag-csharp","tag-linq","tag-mongodb","tag-nosql"],"aioseo_notices":[],"_links":{"self":[{"href":"http:\/\/architester.com\/blog\/wp-json\/wp\/v2\/posts\/95","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/architester.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/architester.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/architester.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/architester.com\/blog\/wp-json\/wp\/v2\/comments?post=95"}],"version-history":[{"count":5,"href":"http:\/\/architester.com\/blog\/wp-json\/wp\/v2\/posts\/95\/revisions"}],"predecessor-version":[{"id":175,"href":"http:\/\/architester.com\/blog\/wp-json\/wp\/v2\/posts\/95\/revisions\/175"}],"wp:attachment":[{"href":"http:\/\/architester.com\/blog\/wp-json\/wp\/v2\/media?parent=95"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/architester.com\/blog\/wp-json\/wp\/v2\/categories?post=95"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/architester.com\/blog\/wp-json\/wp\/v2\/tags?post=95"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}