Entity Sql query many to many relationship + tpt inheritance problemI have a model with tpt inheritance between Person (base) and Guest (derived) entities. There is also many to many relationship between entities Person and Company.<br/> <br/> I have a working LINQ to Entities query, but I need to get this same query working in ESQL with ObjectServices and with EntityClient.<br/> I need to get name of the guest and the name of his company, for every one that has guested more than 1 time.<br/> <br/> I would appreciate complete query with printing out these two data.<br/> <br/> Here is the LINQ query :<br/> <br/> var persons = from c in context.Person.OfType&lt;Guest&gt;()<br/>                             from company in c.Company<br/>                             where c.timesGuested&gt;1<br/>                             select new {c, company};<br/> <br/> foreach(var person in persons)<br/> {<br/>                             Console.WriteLine(&quot;{0} {1}, person.c.Name.Trim(), person.company.Name); <br/> }© 2009 Microsoft Corporation. All rights reserved.Tue, 07 Jul 2009 06:45:10 Zee4332f1-0f04-46aa-b147-e3702bec95c0http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/ee4332f1-0f04-46aa-b147-e3702bec95c0#ee4332f1-0f04-46aa-b147-e3702bec95c0http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/ee4332f1-0f04-46aa-b147-e3702bec95c0#ee4332f1-0f04-46aa-b147-e3702bec95c0Vladimir Rodichttp://social.msdn.microsoft.com/Profile/en-US/?user=Vladimir%20RodicEntity Sql query many to many relationship + tpt inheritance problemI have a model with tpt inheritance between Person (base) and Guest (derived) entities. There is also many to many relationship between entities Person and Company.<br/> <br/> I have a working LINQ to Entities query, but I need to get this same query working in ESQL with ObjectServices and with EntityClient.<br/> I need to get name of the guest and the name of his company, for every one that has guested more than 1 time.<br/> <br/> I would appreciate complete query with printing out these two data.<br/> <br/> Here is the LINQ query :<br/> <br/> var persons = from c in context.Person.OfType&lt;Guest&gt;()<br/>                             from company in c.Company<br/>                             where c.timesGuested&gt;1<br/>                             select new {c, company};<br/> <br/> foreach(var person in persons)<br/> {<br/>                             Console.WriteLine(&quot;{0} {1}, person.c.Name.Trim(), person.company.Name); <br/> }Thu, 02 Jul 2009 12:11:54 Z2009-07-02T12:11:54Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/ee4332f1-0f04-46aa-b147-e3702bec95c0#4194748c-d17d-4037-ad07-52ed23428e0chttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/ee4332f1-0f04-46aa-b147-e3702bec95c0#4194748c-d17d-4037-ad07-52ed23428e0cKati Iceva - MSFThttp://social.msdn.microsoft.com/Profile/en-US/?user=Kati%20Iceva%20-%20MSFTEntity Sql query many to many relationship + tpt inheritance problem<p class=MsoNormal style="line-height:normal;margin:0in 0in 10pt;background:white"><span style="color:black;font-size:12pt"><span style="font-family:Calibri">Hi Vladimir, </span></span></p> <p class=MsoNormal style="line-height:normal;margin:0in 0in 10pt;background:white"><span style="color:black;font-size:12pt"><span style="font-family:Calibri"> </span></span></p> <p class=MsoNormal style="line-height:normal;margin:0in 0in 10pt;background:white"><span style="color:black;font-size:12pt"><span style="font-family:Calibri">The equivalent query in EntitySQL would look something like:</span></span></p> <p class=MsoNormal style="line-height:normal;margin:0in 0in 0pt 6pt;background:white;tab-stops:45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt"><span style="font-family:'Courier New';color:black;font-size:10pt">SELECT c, company</span></p> <p class=MsoNormal style="line-height:normal;margin:0in 0in 0pt 6pt;background:white;tab-stops:45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt"><span style="font-family:'Courier New';color:black;font-size:10pt">FROM OfType(Container.PersonSet, Guest) as c CROSS APPLY c.Company as c</span></p> <p class=MsoNormal style="line-height:normal;margin:0in 0in 0pt 6pt;background:white;tab-stops:45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt"><span style="font-family:'Courier New';color:black;font-size:10pt">WHERE c.timesGuested &gt; 1</span></p> <p class=MsoNormal style="line-height:normal;margin:0in 0in 0pt 6pt;background:white;tab-stops:45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt"><span style="font-family:'Courier New';color:black;font-size:10pt"> </span></p> <p class=MsoNormal style="line-height:normal;margin:0in 0in 10pt;background:white"><span style="color:black;font-size:12pt"><span style="font-family:Calibri">Please note that you would have to adjust ‘Container.PersonSet’ to match the container/entity set and these may differ when the query is issued against Entity Client and the object context. <span style=""> </span>You also may need to prefix the type Guest with the namespace. <span style=""> </span>The EntityClient version would have to match the names specified in your CSDL file (or the CSDL portion of the EDMX) and the object context one names specified in the object context class (e.g. the generated code class).</span></span></p> <p class=MsoNormal style="line-height:normal;margin:0in 0in 10pt;background:white"><span style="color:black;font-size:12pt"><span style="font-family:Calibri"> </span></span></p> <p class=MsoNormal style="line-height:normal;margin:0in 0in 10pt;background:white"><span style="color:black;font-size:12pt"><span style="font-family:Calibri">I hope that helps.</span></span></p> <p class=MsoNormal style="line-height:normal;margin:0in 0in 10pt;background:white"><span style="color:black;font-size:12pt"><span style="font-family:Calibri">Thanks,</span></span></p> <p class=MsoNormal style="line-height:normal;margin:0in 0in 10pt;background:white"><span style="color:black;font-size:12pt"><span style="font-family:Calibri">Kati</span></span></p><hr class="sig">This posting is provided &quot;AS IS&quot; with no warranties, and confers no rights.Thu, 02 Jul 2009 18:12:13 Z2009-07-02T18:12:13Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/ee4332f1-0f04-46aa-b147-e3702bec95c0#38b88485-b660-4e12-819e-742fecc0b806http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/ee4332f1-0f04-46aa-b147-e3702bec95c0#38b88485-b660-4e12-819e-742fecc0b806Vladimir Rodichttp://social.msdn.microsoft.com/Profile/en-US/?user=Vladimir%20RodicEntity Sql query many to many relationship + tpt inheritance problem<span style="text-decoration:underline">EntityClient</span> :<br/> <br/> <p class=MsoNormal style="margin:0in 0in 0pt 6pt;background:white none repeat scroll 0% 0%;line-height:normal"><span style="font-family:'Courier New';color:black;font-size:10pt">SELECT c, company</span></p> <p class=MsoNormal style="margin:0in 0in 0pt 6pt;background:white none repeat scroll 0% 0%;line-height:normal"><span style="font-family:'Courier New';color:black;font-size:10pt">FROM OfType(Container.PersonSet, Guest) as c CROSS APPLY </span> <span style="font-family:'Courier New';color:black;font-size:10pt">Container</span> <span style="font-family:'Courier New';color:black;font-size:10pt">.Company as <strong>c</strong> </span></p> <p class=MsoNormal style="margin:0in 0in 0pt 6pt;background:white none repeat scroll 0% 0%;line-height:normal"><span style="font-family:'Courier New';color:black;font-size:10pt">WHERE c.timesGuested &gt; 1</span></p> <br/> <span style="font-family:'Courier New';color:black;font-size:10pt">I get foolowing error: The alias 'c' was already used. in the current FROM clause, near simple identifier<br/> <br/> Then I've tried with (I guessed you might have made a small mistake in your query) :<br/> <br/> </span> <span style="font-family:'Courier New';color:black;font-size:10pt">SELECT c, company</span> <p class=MsoNormal style="margin:0in 0in 0pt 6pt;background:white none repeat scroll 0% 0%;line-height:normal"><span style="font-family:'Courier New';color:black;font-size:10pt">FROM OfType(Container.PersonSet, Guest) as c CROSS APPLY </span> <span style="font-family:'Courier New';color:black;font-size:10pt">Container</span> <span style="font-family:'Courier New';color:black;font-size:10pt">.Company as <strong>company</strong> </span></p> <p class=MsoNormal style="margin:0in 0in 0pt 6pt;background:white none repeat scroll 0% 0%;line-height:normal"><span style="font-family:'Courier New';color:black;font-size:10pt">WHERE c.timesGuested &gt; 1</span></p> <span style="font-family:'Courier New';color:black;font-size:10pt"><br/> and then for the following code to print value<br/> <br/> EntityCommand cmd = conn.CreateCommand();<br/> cmd.CommandText = queryString;<br/> using (EntityDataReader rdr =<br/>      cmd.ExecuteReader(System.Data.CommandBehavior.SequentialAccess))<br/>      {<br/>            while (rdr.Read())<br/>            {<br/>                 Console.WriteLine(&quot;{0}&quot;, rdr.GetString(0));<br/> <br/> get the following error when I try to print:<br/> <br/> Unable to cast object of type 'System.Data.Query.ResultAssembly.BridgeDataRecord' to type 'System.String'<br/> <br/> <br/> <br/> <span style="text-decoration:underline">ObjectContext</span> :<br/> <br/> how am I suppose to pring out the results ?<br/> <br/> var persons = context.CreateQuery&lt;DbDataRecord&gt;(qStr);<br/> <br/> foreach (var person in persons)<br/> {<br/> ??? now what ???<br/> how do I print out Person.Name and Company.Name  ???<br/> <br/> Do I need to cast DbDataRecord result ? And how do I do that exactly cause I could not find it... and I have two Entities returned c representing Person and company representing Company<br/> <br/> </span> <span style="font-family:'Courier New';color:black;font-size:10pt"><br/> <br/> </span>Fri, 03 Jul 2009 15:58:00 Z2009-07-03T15:58:00Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/ee4332f1-0f04-46aa-b147-e3702bec95c0#0dc1ef03-8289-4c3f-a89e-13fa8c315bb0http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/ee4332f1-0f04-46aa-b147-e3702bec95c0#0dc1ef03-8289-4c3f-a89e-13fa8c315bb0Daniel Simmons - MSFThttp://social.msdn.microsoft.com/Profile/en-US/?user=Daniel%20Simmons%20-%20MSFTEntity Sql query many to many relationship + tpt inheritance problem<p>With entity client when you retrieve an entity, that entity will be represented by a DataRecord embedded in a column within the outer DataRecord.  So, the exception you are getting is saying that you are retrieving the first column as a string when it's actually an object which is a datarecord.  So probably you could change your sample code to be something like this:<br/><br/><span style="font-family:Courier New;font-size:x-small">EntityCommand cmd = conn.CreateCommand();<br/>cmd.CommandText = queryString;<br/>using (EntityDataReader rdr =<br/>     cmd.ExecuteReader(System.Data.CommandBehavior.SequentialAccess))<br/>     {<br/>           while (rdr.Read())<br/>           {<br/>                var guestRecord = (DbDataRecord) rdr.GetObject(0);<br/>                Console.WriteLine(&quot;{0}&quot;, guestRecord.GetString(0));<br/>                var companyRecord = (DbDataRecord) rdr.GetObject(1);<br/>                ...<br/><br/>When you do this with ObjectServices, the difference is that the entities are materialized into objects instead of data records, so the code would be something like this:<br/><br/>var persons = context.CreateQuery&lt;DbDataRecord&gt;(qStr);<br/><br/>foreach (var record in persons)<br/>{<br/>    var guest = (Guest) record.GetObject(0);<br/>    var company = (Company) record.GetObject(1);<br/><br/>    Console.WriteLine(guest.Name);<br/>    Console.WriteLine(company.Name);<br/>}<br/><br/>- Danny<br/><br/></span></p><hr class="sig">This posting is provided &quot;AS IS&quot; with no warranties, and confers no rights.Sat, 04 Jul 2009 07:37:23 Z2009-07-04T07:37:23Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/ee4332f1-0f04-46aa-b147-e3702bec95c0#ff698c14-b10e-4d10-a3a5-241e55302864http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/ee4332f1-0f04-46aa-b147-e3702bec95c0#ff698c14-b10e-4d10-a3a5-241e55302864Vladimir Rodichttp://social.msdn.microsoft.com/Profile/en-US/?user=Vladimir%20RodicEntity Sql query many to many relationship + tpt inheritance problemfor some reason i do not have getObject method so I get following errors for ObjectContext and EntityClient<br/> <br/> Error    1    'System.Data.Common.DbDataRecord' does not contain a definition for 'GetObject' and no extension method 'GetObject' accepting a first argument of type 'System.Data.Common.DbDataRecord' could be found (are you missing a using directive or an assembly reference?)    C:\Users\Vladimir\Documents\Visual Studio 2008\Projects\PrimerZaEntity\PrimerZaEntity\Program.cs    82    46    PrimerZaEntity<br/> <br/> <br/> Error    2    'System.Data.EntityClient.EntityDataReader' does not contain a definition for 'GetObject' and no extension method 'GetObject' accepting a first argument of type 'System.Data.EntityClient.EntityDataReader' could be found (are you missing a using directive or an assembly reference?)    C:\Users\Vladimir\Documents\Visual Studio 2008\Projects\PrimerZaEntity\PrimerZaEntity\Program.cs    115    61    PrimerZaEntity<br/> <br/> <br/> here are the using directives :<br/> <br/> using System;<br/> using System.Data;<br/> using System.Data.SqlClient;<br/> using System.Data.SqlServerCe;<br/> <br/> using System.Collections.Generic;<br/> using System.Linq;<br/> using System.Text;<br/> <br/> using System.Data.EntityClient;<br/> using System.Data.Common;Sat, 04 Jul 2009 15:13:06 Z2009-07-04T15:13:06Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/ee4332f1-0f04-46aa-b147-e3702bec95c0#16479770-a2b8-4d6f-a254-513d115560e8http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/ee4332f1-0f04-46aa-b147-e3702bec95c0#16479770-a2b8-4d6f-a254-513d115560e8Daniel Simmons - MSFThttp://social.msdn.microsoft.com/Profile/en-US/?user=Daniel%20Simmons%20-%20MSFTEntity Sql query many to many relationship + tpt inheritance problemSorry I was in a hurry and just typed from memory rather than looking up the command or writing a simple repro.  What you really want is GetValue rather than GetObject.<br/><br/>- Danny<hr class="sig">This posting is provided &quot;AS IS&quot; with no warranties, and confers no rights.Sat, 04 Jul 2009 16:50:30 Z2009-07-04T16:50:30Z