{"id":385,"date":"2019-02-07T12:56:56","date_gmt":"2019-02-07T16:56:56","guid":{"rendered":"https:\/\/benjaminray.com\/codebase\/?p=385"},"modified":"2019-02-11T12:06:44","modified_gmt":"2019-02-11T16:06:44","slug":"selecting-data-with-entity-framework","status":"publish","type":"post","link":"https:\/\/benjaminray.com\/codebase\/selecting-data-with-entity-framework\/","title":{"rendered":"Selecting Data with Entity Framework"},"content":{"rendered":"<h2>Preface<\/h2>\n<p>Please see <a href=\"https:\/\/benjaminray.com\/codebase\/entity-framework-select-functions-explained\/\">Entity Framework Select Functions Explained<\/a> for an overview of each available select method. This post only covers a handful of them.<\/p>\n<h2>Selecting Single Record by Primary Key<\/h2>\n<p>Returns null if object with matching key not found.<\/p>\n<p>Example with simple PK:<\/p>\n<pre><code>Widgets widget;\nusing (WidgetModel ctx = new WidgetModel()) {\n    widget = ctx.Widgets.Find(SomeID);\n}\nreturn widget;\n<\/code><\/pre>\n<p>Example with composite PK (pass parameters in correct order):<\/p>\n<pre><code>UserWidgets uw;\nusing (WidgetModel ctx = new WidgetModel()) {\n    uw = ctx.UserWidgets.Find(UserID, WidgetID);\n}\nreturn uw;\n<\/code><\/pre>\n<h2>Selecting Multiple Records in a Table (with 'where' clause)<\/h2>\n<pre><code>List&lt;Widgets&gt; shifts = new List&lt;Widgets&gt;();\nusing (WidgetModel ctx = new WidgetModel()) {\n    widgets = ctx.Widgets\n        .Where(x =&gt; x.Prop1 == \"Value1\" || x.Prop1 == \"Value2\")\n        .ToList();\n}\nreturn widgets;\n<\/code><\/pre>\n<h2>Selecting All Records in a Table (no 'where' clause)<\/h2>\n<pre><code>List&lt;Widgets&gt; shifts = new List&lt;Widgets&gt;();\nusing (WidgetModel ctx = new WidgetModel()) {\n    widgets = ctx.Widgets.ToList();\n}\nreturn widgets;\n<\/code><\/pre>\n<h2>Selecting a Single Object<\/h2>\n<p>Example:<\/p>\n<pre><code>Widgets widget;\nusing (WidgetModel ctx = new WidgetModel()) {\n    widget = ctx.Widgets\n        .Where(x =&gt; x.Date == SomeDate)\n        .Where(x =&gt; x.UserID == SomeUserID)\n        .SingleOrDefault();\n}\n<\/code><\/pre>\n<p>Note: If you are selecting a single object and there should never be multiple objects returned, use .SingleOrDefault(). If multiple objects are returned, the following exception will be thrown:<\/p>\n<blockquote><p>\n  System.InvalidOperationException: Sequence contains more than one element\n<\/p><\/blockquote>\n<p>Likewise, if you are selecting a single object and there should never be a null object returned, use .Single(). If no object is returned, the following exception will be thrown:<\/p>\n<blockquote><p>\n  System.InvalidOperationException: Sequence contains no elements\n<\/p><\/blockquote>\n<h2>Selecting First of Multiple Objects<\/h2>\n<p>Example:<\/p>\n<pre><code>Widgets widget;\nusing (WidgetModel ctx = new WidgetModel()) {\n    widget = ctx.Widgets\n        .Where(x =&gt; x.Date == SomeDate)\n        .OrderBy(x =&gt; x.Prop1)\n        .FirstOrDefault();\n}\n<\/code><\/pre>\n<h2>Controlling Order<\/h2>\n<p>Order by two columns - ascending:<\/p>\n<pre><code>Widgets widget;\nusing (WidgetModel ctx = new WidgetModel()) {\n    widget = ctx.Widgets\n        .Where(x =&gt; x.Date == SomeDate)\n        .OrderBy(x =&gt; x.Prop1)\n        .ThenBy(x =&gt; x.Prop2)\n        .ToList();\n}\n<\/code><\/pre>\n<p>Order by two columns - descending:<\/p>\n<pre><code>Widgets widget;\nusing (WidgetModel ctx = new WidgetModel()) {\n    widget = ctx.Widgets\n        .Where(x =&gt; x.Date == SomeDate)\n        .OrderByDescending(x =&gt; x.Prop1)\n        .ThenByDescending(x =&gt; x.Prop2)\n        .ToList();\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Preface Please see Entity Framework Select Functions Explained for an overview of each available select method. This post only covers a handful of them. Selecting Single Record by Primary Key  [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[19],"tags":[],"class_list":["post-385","post","type-post","status-publish","format-standard","hentry","category-entity-framework"],"acf":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9GNjN-6d","jetpack_likes_enabled":false,"_links":{"self":[{"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/posts\/385","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/comments?post=385"}],"version-history":[{"count":14,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/posts\/385\/revisions"}],"predecessor-version":[{"id":416,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/posts\/385\/revisions\/416"}],"wp:attachment":[{"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/media?parent=385"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/categories?post=385"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/tags?post=385"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}