{"id":1050,"date":"2017-03-26T17:17:08","date_gmt":"2017-03-26T21:17:08","guid":{"rendered":"http:\/\/www.ObiOberoi.com\/?p=1050"},"modified":"2017-03-26T17:17:08","modified_gmt":"2017-03-26T21:17:08","slug":"can-use-projections-ef-core-absolutely","status":"publish","type":"post","link":"https:\/\/obioberoi.com\/?p=1050","title":{"rendered":"Can I use Projections in EF Core&#8230;Absolutely!!"},"content":{"rendered":"<p>Before we drill into Projections, let&#8217;s find out what Projections really are:<\/p>\n<p>Projection(s) is a powerful feature of LINQ that enables you to perform query operations on a data source, collection etc. It shapes the results of the data being queried into a different structure\/format that the original data source is in.<\/p>\n<p>It is a process to express queries in order to return a desired set of data other than the entire entity. Projection queries improve the efficiency of your application by only retrieving specific fields from your database.<\/p>\n<p>A projection query lets you select only the data that is required from your model. It is done, by loading the data into a custom or anonymous type. This significantly improves the performance of your application, especially when you filter out the fields that contain huge amounts of data.<\/p>\n<p>When you use the <em>Select <\/em>method, you can retrieve scalar properties and also navigation properties to retrieve data in the same query. Please see the code snippet below:<\/p>\n<pre>   private static void Projections()\n        {\n            using (var db = new demo_departmentContext())\n            {\n                var dept = db.Departments.Select(d => new { d.DepId, d.DepName, d.Location })\n                    .Where(d => d.DepName.Contains(\"IT\"))\n                    .ToList();\n                foreach (var item in dept)\n                {\n                    Console.WriteLine(\"{0} - {1} - {2}\", item.DepId, item.DepName, item.Location);\n                }\n                Console.ReadLine();\n            }\n        }\n<\/pre>\n<p>And following is the output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1053\" src=\"\/wp-content\/uploads\/2017\/03\/EFCoreProjectionsOutput-1.jpg\" alt=\"EFCoreProjectionsOutput\" width=\"268\" height=\"104\" \/><span style=\"color: #333333; font-family: Georgia, 'Bitstream Charter', serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: #ffffff; display: inline !important; float: none;\">As\u00a0 you saw above using projections\u00a0allow us to retrieve a subset of the properties from an object as opposed to the entire object. This causes a smaller amount of data to be returned in the query.<\/span> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Before we drill into Projections, let&#8217;s find out what Projections really are: Projection(s) is a powerful feature of LINQ that enables you to perform query operations on a data source, collection etc. It shapes the results of the data being &hellip; <a href=\"https:\/\/obioberoi.com\/?p=1050\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-1050","post","type-post","status-publish","format-standard","hentry","category-orm"],"_links":{"self":[{"href":"https:\/\/obioberoi.com\/index.php?rest_route=\/wp\/v2\/posts\/1050","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/obioberoi.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/obioberoi.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/obioberoi.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/obioberoi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1050"}],"version-history":[{"count":0,"href":"https:\/\/obioberoi.com\/index.php?rest_route=\/wp\/v2\/posts\/1050\/revisions"}],"wp:attachment":[{"href":"https:\/\/obioberoi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1050"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/obioberoi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1050"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/obioberoi.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1050"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}