Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq SELECT with ExecuteQuery

Tags:

c#

sql

asp.net

linq

I get the error:

The type 'System.Int32[]' must declare a default (parameterless) constructor in order to be constructed during mapping.

With the code:

var gamePlayRecord = db.ExecuteQuery<int[]>("SELECT UserID, IPID, GameID FROM ArcadeGames WHERE ID = " + gamePlayRecordID).Single();
var userID = gamePlayRecord[0];
var ipID = gamePlayRecord[1];
var gameID = gamePlayRecord[2];

I know this is wrong, but can someone show me how to do it correctly without needing to create a new object preferably?

like image 213
Tom Gullen Avatar asked Jul 12 '13 12:07

Tom Gullen


2 Answers

Result of this query is not int[] but one row with numbers.

not good solution: use for every number:

int userID = db.ExecuteQuery<int>("SELECT UserID FROM ArcadeGames WHERE ID = " + gamePlayRecordID).Single();
int ipID = db.ExecuteQuery<int>("SELECT IPID FROM ArcadeGames WHERE ID = " + gamePlayRecordID).Single();
int gameID db.ExecuteQuery<int>("SELECT GameID FROM ArcadeGames WHERE ID = " + gamePlayRecordID).Single();

or Create sql query

db.ExecuteQuery<int>(@"
SELECT UserID FROM ArcadeGames WHERE ID = {0}
UNION ALL
SELECT IPID FROM ArcadeGames WHERE ID = {0}
UNION ALL
SELECT GameID FROM ArcadeGames WHERE ID = {0}", 
gamePlayRecordID).ToList();

or Create class ...

like image 67
slavoo Avatar answered Sep 18 '22 23:09

slavoo


I think I have misunderstood the question a little bit. But as @goric explained: An ORM mapper wants to map the results to an object. If you don't want an object or class than don't use an ORM mapper, but use the basic SqlDataReader.

SqlCommand command = new SqlCommand("SELECT UserID, IPID, GameID FROM ArcadeGames WHERE ID = " + gamePlayRecordID, connection);
SqlDataReader reader = command.ExecuteReader();
if (reader.Read())
{
  var userID = reader[0];
  var ipID = reader[1];
  var gameID = reader[2];
}
like image 31
Andre van Heerwaarde Avatar answered Sep 20 '22 23:09

Andre van Heerwaarde