<p>This query works great:</p>

<pre><code>var pageObject = (from op in db.ObjectPermissions
                  join pg in db.Pages on op.ObjectPermissionName equals page.PageName
                  where pg.PageID == page.PageID
                  select op)
                 .SingleOrDefault();
</code></pre>

<p>I get a new type with my 'op' fields.  Now I want to retrieve my 'pg' fields as well, but</p>

<pre><code>select op, pg).SingleOrDefault();
</code></pre>

<p>doesn't work.</p>

<p>How can I select everything from both tables so that they appear in my new pageObject type?</p>

[{'answer_id': 32445, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 2, 'selected': False, 'text': '<p>You must create a new anonymous type:</p>\n\n<pre><code> select new { op, pg }\n</code></pre>\n\n<p>Refer to the official <a href="http://msdn.microsoft.com/en-us/library/bb397696.aspx" rel="nofollow noreferrer">guide</a>.</p>\n'}, {'answer_id': 32449, 'author': 'aku', 'author_id': 1196, 'author_profile': 'https://Stackoverflow.com/users/1196', 'pm_score': 1, 'selected': False, 'text': '<p>change </p>\n\n<pre><code>select op) \n</code></pre>\n\n<p>to</p>\n\n<pre><code>select new { op, pg })\n</code></pre>\n'}, {'answer_id': 32465, 'author': 'ljs', 'author_id': 3394, 'author_profile': 'https://Stackoverflow.com/users/3394', 'pm_score': 8, 'selected': True, 'text': '<p>You can use anonymous types for this, i.e.:</p>\n\n<pre><code>var pageObject = (from op in db.ObjectPermissions\n                  join pg in db.Pages on op.ObjectPermissionName equals page.PageName\n                  where pg.PageID == page.PageID\n                  select new { pg, op }).SingleOrDefault();\n</code></pre>\n\n<p>This will make pageObject into an IEnumerable of an anonymous type so AFAIK you won\'t be able to pass it around to other methods, however if you\'re simply obtaining data to play with in the method you\'re currently in it\'s perfectly fine. You can also name properties in your anonymous type, i.e.:-</p>\n\n<pre><code>var pageObject = (from op in db.ObjectPermissions\n                  join pg in db.Pages on op.ObjectPermissionName equals page.PageName\n                  where pg.PageID == page.PageID\n                  select new\n                  {\n                      PermissionName = pg, \n                      ObjectPermission = op\n                  }).SingleOrDefault();\n</code></pre>\n\n<p>This will enable you to say:-</p>\n\n<pre><code>if (pageObject.PermissionName.FooBar == "golden goose") Application.Exit();\n</code></pre>\n\n<p>For example :-)</p>\n'}, {'answer_id': 144219, 'author': 'Codewerks', 'author_id': 17729, 'author_profile': 'https://Stackoverflow.com/users/17729', 'pm_score': 3, 'selected': False, 'text': "<p>If you don't want to use anonymous types b/c let's say you're passing the object to another method, you can use the LoadWith load option to load associated data. It requires that your tables are associated either through foreign keys or in your Linq-to-SQL dbml model.</p>\n\n<pre><code>db.DeferredLoadingEnabled = false;\nDataLoadOptions dlo = new DataLoadOptions();\ndlo.LoadWith&lt;ObjectPermissions&gt;(op =&gt; op.Pages)\ndb.LoadOptions = dlo;\n\nvar pageObject = from op in db.ObjectPermissions\n         select op;\n\n// no join needed\n</code></pre>\n\n<p>Then you can call </p>\n\n<pre><code>pageObject.Pages.PageID\n</code></pre>\n\n<p>Depending on what your data looks like, you'd probably want to do this the other way around,</p>\n\n<pre><code>DataLoadOptions dlo = new DataLoadOptions();\ndlo.LoadWith&lt;Pages&gt;(p =&gt; p.ObjectPermissions)\ndb.LoadOptions = dlo;\n\nvar pageObject = from p in db.Pages\n                 select p;\n\n// no join needed\n\nvar objectPermissionName = pageObject.ObjectPermissions.ObjectPermissionName;\n</code></pre>\n"}, {'answer_id': 10050917, 'author': 'daviddeath', 'author_id': 1277335, 'author_profile': 'https://Stackoverflow.com/users/1277335', 'pm_score': 2, 'selected': False, 'text': '<p>If the anonymous type causes trouble for you, you can create a simple data class:</p>\n\n<pre><code>public class PermissionsAndPages\n{\n     public ObjectPermissions Permissions {get;set}\n     public Pages Pages {get;set}\n}\n</code></pre>\n\n<p>and then in your query:</p>\n\n<pre><code>select new PermissionsAndPages { Permissions = op, Page = pg };\n</code></pre>\n\n<p>Then you can pass this around:</p>\n\n<pre><code>return queryResult.SingleOrDefault(); // as PermissionsAndPages\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/32433', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3047/']