I am a newbie with Dapper and I copied this SQL Script(from a test that I was doing with Entity Framework) and tried to return a List of theses rows, but its returning the entire Class and not just the ones that I want!
using (IDbConnection connection = new SqlConnection(@"Data Source=DESKTOP-CD2UQI5\SQLEXPRESS;Initial Catalog=ServerContext;Integrated Security=True"))
{
var res = connection.Query<Table>("select Images,AddressFrom, from Table where Table.id = 1").ToList();
response = Request.CreateResponse(HttpStatusCode.OK, res);
}
Please help.
Dapper doesn't do any processing of your SQL (well, it does in some very very limited scenarios). So: whatever is being returned is because of your query - it isn't changing the query to remove the where
clause, and it isn't inventing data.
If you mean it is returning more columns than you expect, then again: check your query. The one shown in the question isn't valid (trailing comma in the select
), so I can't really comment based on the question. Check for a rogue *
.
So: take you query, and try running it SSMS or another SQL tool; see which rows and columns come back.
If Table.id
is a unique / primary / identity / etc, then yes it seems odd to get more than one, but: I can't tell what is going on from here, except: dapper is simply running your query.
Minor points:
AsList()
would be preferable to ToList()
QuerySingle
etcif the id
isn't fixed, you probably want to use a parameter, for example:
int id = ...
var res = connection.Query<Table>(
"select Images,AddressFrom, from Table where Table.id = @id", new { id }
).AsList();