ExecuteQuery

Discussion of open issues, suggestions and bugs regarding LinqConnect – Devart's LINQ to SQL compatible ORM
Post Reply
Sulphy
Posts: 34
Joined: Wed 12 Sep 2012 18:07

ExecuteQuery

Post by Sulphy » Wed 26 Feb 2014 19:20

Hi all,

Just wondering if anyone else has seen this before. It might just be me as it's late in the day.

I've writen a code block where I can pass in a value which is evaluated by a switch statement to then set a variable accordingly.

This variable is used to access a database table via the ExecuteQuery method.

Code: Select all

 var getVolume = context.ExecuteQuery(typeof(int),"SELECT COUNT(*) as vol FROM my_table1").ToString().FirstOrDefault();
I have changed the variable types but for this example i've left as 'var'. Instead of getting the 4 million odd records returned as a total count I get: 68 'D' returned. When using the Linq to SQL approach and the table specifically stated in my code everything works fine. However in the interests of code efficiency I wanted pass the table name dynamically hence using the ExecuteQuery method.

thanks in advance!

MariiaI
Devart Team
Posts: 1472
Joined: Mon 13 Feb 2012 08:17

Re: ExecuteQuery

Post by MariiaI » Thu 27 Feb 2014 10:49

Please rewrite your query in one of the following ways:
1)

Code: Select all

var getVolume = ctx.ExecuteQuery(typeof(int), "SELECT COUNT(*) as vol FROM my_table1").OfType<int>().FirstOrDefault().ToString();
2)

Code: Select all

var getVolume = ctx.ExecuteQuery<Int32>("SELECT COUNT(*) as vol FROM my_table1").FirstOrDefault().ToString();
3)

Code: Select all

int getVolume = ctx.ExecuteQuery<Int32>("SELECT COUNT(*) as vol FROM my_table1").FirstOrDefault();
You are getting such a result due to the fact, that your query first converts unenumerated result to a string and only after that you are trying to get the first item (FirstOrDefault). To get the correct results, you should first enumerate the collection of the resulting items and only then convert it to the a string format.

Post Reply