Note that there are some explanatory texts on larger screens.

plurals
  1. PODataContext.ExecuteQuery(CustomType, Query) returns IEnumerable of initialized CustomType
    primarykey
    data
    text
    <p>I need to execute runtime defined SQL queries on a database and show results on a datagrid. I'm using this code to query the database. It should return an IEnumerable of runtime defined objects of customType</p> <pre><code>List&lt;dynamic&gt; result; using (context = new DataContext(ConnStr)) { IEnumerable enumerable = context.ExecuteQuery(customType, query); result = enumerable.Cast&lt;dynamic&gt;().ToList(); } </code></pre> <p>The sample customType and Query I'm using are as follows</p> <p>CustomType: CustomType is created at runtime and it's equivalent to:</p> <pre><code>class customType { public int Id {get; set;} } </code></pre> <p>Query: </p> <pre><code>Select id from TestTable </code></pre> <p>What ExecuteQuery is returning a list of customType objects that id is 0 in all of them. I did the same with context.ExecuteQuery and this time I defined my customType in compile time (&lt;> operator does not work with Type instances), and it worked as expected and returned list of legit ids. I checked msdn page and find this interesting description:</p> <blockquote> <p>Public method <em>ExecuteQuery(String, Object[])</em> Executes SQL queries directly on the database <strong>and returns objects</strong>. </p> <p>Public method <em>ExecuteQuery(Type, String, Object[])</em> Executes SQL queries directly on the database.</p> </blockquote> <p>Any help is appreciated. </p> <p>Update: TestTable definition in my SQL Server database:</p> <pre><code>CREATE TABLE [dbo].[TestTable]( [Id] [int] IDENTITY(1,1) NOT NULL, [StartDate] [datetime] NOT NULL, [BasePath] [varchar](512) NOT NULL, CONSTRAINT [PK_TestTable] PRIMARY KEY CLUSTERED ( [Id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] </code></pre> <p>(if there's any syntax error it's a typo)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload