Dapper QuerySingleOrDefault doesn't return null
Why Dapper QuerySingleOrDefault doesn't return null?
I use hard qwery.
In result I have object with default fields.
I ran this query in DB Browser for SQLite, It returned 0 rows.
public static Sensor GetSensor(string ip, string sensorName)
{
string sql = @"SELECT ip, name, invert, enable FROM DeviceAndOids AS A JOIN
DeviceForMonitoring AS B ON A.deviceForMonitoringKey=B.key JOIN
DeviceTypes AS C ON B.deviceTypeId=C.id WHERE
oidForDeviceKey IN (SELECT key FROM OidsForDevice WHERE
deviceTypeId IN (SELECT deviceTypeId FROM DeviceForMonitoring WHERE
ip = @ip) AND
name = @sensorName)";
using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString()))
{
var result = cnn.QuerySingleOrDefault<Sensor>(sql, new { ip, sensorName });
return result;
}
}
c# dapper
|
show 7 more comments
Why Dapper QuerySingleOrDefault doesn't return null?
I use hard qwery.
In result I have object with default fields.
I ran this query in DB Browser for SQLite, It returned 0 rows.
public static Sensor GetSensor(string ip, string sensorName)
{
string sql = @"SELECT ip, name, invert, enable FROM DeviceAndOids AS A JOIN
DeviceForMonitoring AS B ON A.deviceForMonitoringKey=B.key JOIN
DeviceTypes AS C ON B.deviceTypeId=C.id WHERE
oidForDeviceKey IN (SELECT key FROM OidsForDevice WHERE
deviceTypeId IN (SELECT deviceTypeId FROM DeviceForMonitoring WHERE
ip = @ip) AND
name = @sensorName)";
using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString()))
{
var result = cnn.QuerySingleOrDefault<Sensor>(sql, new { ip, sensorName });
return result;
}
}
c# dapper
As you can see in the documentation, if there's no item it will return the default (nothing), if there's one item it will return the item and if there's many items it will return a exception. So it's not returningnull
because it's not supposed to returnnull
.
– ikerbera
Nov 20 '18 at 8:47
1
@ikerbera, thanks, I was reading. Вut what is default in my case? I thought default for object is null
– Дмитрий Суворов
Nov 20 '18 at 8:54
What is the value inresult
? You probably have aSensor
class variable with all it's properties at default value. ints at 0, strings at null, bools as false and so on.
– ikerbera
Nov 20 '18 at 9:00
@ikerbera I would like use if (result != null) { // do something}
– Дмитрий Суворов
Nov 20 '18 at 9:04
Can you show yourSensor
class? I'm sure we can find some property that you can check to know if you got no data.
– ikerbera
Nov 20 '18 at 9:09
|
show 7 more comments
Why Dapper QuerySingleOrDefault doesn't return null?
I use hard qwery.
In result I have object with default fields.
I ran this query in DB Browser for SQLite, It returned 0 rows.
public static Sensor GetSensor(string ip, string sensorName)
{
string sql = @"SELECT ip, name, invert, enable FROM DeviceAndOids AS A JOIN
DeviceForMonitoring AS B ON A.deviceForMonitoringKey=B.key JOIN
DeviceTypes AS C ON B.deviceTypeId=C.id WHERE
oidForDeviceKey IN (SELECT key FROM OidsForDevice WHERE
deviceTypeId IN (SELECT deviceTypeId FROM DeviceForMonitoring WHERE
ip = @ip) AND
name = @sensorName)";
using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString()))
{
var result = cnn.QuerySingleOrDefault<Sensor>(sql, new { ip, sensorName });
return result;
}
}
c# dapper
Why Dapper QuerySingleOrDefault doesn't return null?
I use hard qwery.
In result I have object with default fields.
I ran this query in DB Browser for SQLite, It returned 0 rows.
public static Sensor GetSensor(string ip, string sensorName)
{
string sql = @"SELECT ip, name, invert, enable FROM DeviceAndOids AS A JOIN
DeviceForMonitoring AS B ON A.deviceForMonitoringKey=B.key JOIN
DeviceTypes AS C ON B.deviceTypeId=C.id WHERE
oidForDeviceKey IN (SELECT key FROM OidsForDevice WHERE
deviceTypeId IN (SELECT deviceTypeId FROM DeviceForMonitoring WHERE
ip = @ip) AND
name = @sensorName)";
using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString()))
{
var result = cnn.QuerySingleOrDefault<Sensor>(sql, new { ip, sensorName });
return result;
}
}
c# dapper
c# dapper
asked Nov 20 '18 at 8:44
Дмитрий Суворов
315
315
As you can see in the documentation, if there's no item it will return the default (nothing), if there's one item it will return the item and if there's many items it will return a exception. So it's not returningnull
because it's not supposed to returnnull
.
– ikerbera
Nov 20 '18 at 8:47
1
@ikerbera, thanks, I was reading. Вut what is default in my case? I thought default for object is null
– Дмитрий Суворов
Nov 20 '18 at 8:54
What is the value inresult
? You probably have aSensor
class variable with all it's properties at default value. ints at 0, strings at null, bools as false and so on.
– ikerbera
Nov 20 '18 at 9:00
@ikerbera I would like use if (result != null) { // do something}
– Дмитрий Суворов
Nov 20 '18 at 9:04
Can you show yourSensor
class? I'm sure we can find some property that you can check to know if you got no data.
– ikerbera
Nov 20 '18 at 9:09
|
show 7 more comments
As you can see in the documentation, if there's no item it will return the default (nothing), if there's one item it will return the item and if there's many items it will return a exception. So it's not returningnull
because it's not supposed to returnnull
.
– ikerbera
Nov 20 '18 at 8:47
1
@ikerbera, thanks, I was reading. Вut what is default in my case? I thought default for object is null
– Дмитрий Суворов
Nov 20 '18 at 8:54
What is the value inresult
? You probably have aSensor
class variable with all it's properties at default value. ints at 0, strings at null, bools as false and so on.
– ikerbera
Nov 20 '18 at 9:00
@ikerbera I would like use if (result != null) { // do something}
– Дмитрий Суворов
Nov 20 '18 at 9:04
Can you show yourSensor
class? I'm sure we can find some property that you can check to know if you got no data.
– ikerbera
Nov 20 '18 at 9:09
As you can see in the documentation, if there's no item it will return the default (nothing), if there's one item it will return the item and if there's many items it will return a exception. So it's not returning
null
because it's not supposed to return null
.– ikerbera
Nov 20 '18 at 8:47
As you can see in the documentation, if there's no item it will return the default (nothing), if there's one item it will return the item and if there's many items it will return a exception. So it's not returning
null
because it's not supposed to return null
.– ikerbera
Nov 20 '18 at 8:47
1
1
@ikerbera, thanks, I was reading. Вut what is default in my case? I thought default for object is null
– Дмитрий Суворов
Nov 20 '18 at 8:54
@ikerbera, thanks, I was reading. Вut what is default in my case? I thought default for object is null
– Дмитрий Суворов
Nov 20 '18 at 8:54
What is the value in
result
? You probably have a Sensor
class variable with all it's properties at default value. ints at 0, strings at null, bools as false and so on.– ikerbera
Nov 20 '18 at 9:00
What is the value in
result
? You probably have a Sensor
class variable with all it's properties at default value. ints at 0, strings at null, bools as false and so on.– ikerbera
Nov 20 '18 at 9:00
@ikerbera I would like use if (result != null) { // do something}
– Дмитрий Суворов
Nov 20 '18 at 9:04
@ikerbera I would like use if (result != null) { // do something}
– Дмитрий Суворов
Nov 20 '18 at 9:04
Can you show your
Sensor
class? I'm sure we can find some property that you can check to know if you got no data.– ikerbera
Nov 20 '18 at 9:09
Can you show your
Sensor
class? I'm sure we can find some property that you can check to know if you got no data.– ikerbera
Nov 20 '18 at 9:09
|
show 7 more comments
1 Answer
1
active
oldest
votes
Sensor
was struct
. I changed it to class
It works now
Note: it might have worked (renull
) if you had used<Sensor?>
instead of<Sensor>
- however, I strongly suspect that usingstruct
for something that represents a data row is probably a bad idea that could cause multiple subtle confusions
– Marc Gravell♦
Nov 20 '18 at 15:15
Maybe, I didn't understand you very well. But I changed it toclass
. I didn't use<Sensor?>
– Дмитрий Суворов
Nov 20 '18 at 16:39
fair enough; changing it toclass
is by far the best solution here
– Marc Gravell♦
Nov 20 '18 at 16:48
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53389172%2fdapper-querysingleordefault-doesnt-return-null%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sensor
was struct
. I changed it to class
It works now
Note: it might have worked (renull
) if you had used<Sensor?>
instead of<Sensor>
- however, I strongly suspect that usingstruct
for something that represents a data row is probably a bad idea that could cause multiple subtle confusions
– Marc Gravell♦
Nov 20 '18 at 15:15
Maybe, I didn't understand you very well. But I changed it toclass
. I didn't use<Sensor?>
– Дмитрий Суворов
Nov 20 '18 at 16:39
fair enough; changing it toclass
is by far the best solution here
– Marc Gravell♦
Nov 20 '18 at 16:48
add a comment |
Sensor
was struct
. I changed it to class
It works now
Note: it might have worked (renull
) if you had used<Sensor?>
instead of<Sensor>
- however, I strongly suspect that usingstruct
for something that represents a data row is probably a bad idea that could cause multiple subtle confusions
– Marc Gravell♦
Nov 20 '18 at 15:15
Maybe, I didn't understand you very well. But I changed it toclass
. I didn't use<Sensor?>
– Дмитрий Суворов
Nov 20 '18 at 16:39
fair enough; changing it toclass
is by far the best solution here
– Marc Gravell♦
Nov 20 '18 at 16:48
add a comment |
Sensor
was struct
. I changed it to class
It works now
Sensor
was struct
. I changed it to class
It works now
answered Nov 20 '18 at 9:45
Дмитрий Суворов
315
315
Note: it might have worked (renull
) if you had used<Sensor?>
instead of<Sensor>
- however, I strongly suspect that usingstruct
for something that represents a data row is probably a bad idea that could cause multiple subtle confusions
– Marc Gravell♦
Nov 20 '18 at 15:15
Maybe, I didn't understand you very well. But I changed it toclass
. I didn't use<Sensor?>
– Дмитрий Суворов
Nov 20 '18 at 16:39
fair enough; changing it toclass
is by far the best solution here
– Marc Gravell♦
Nov 20 '18 at 16:48
add a comment |
Note: it might have worked (renull
) if you had used<Sensor?>
instead of<Sensor>
- however, I strongly suspect that usingstruct
for something that represents a data row is probably a bad idea that could cause multiple subtle confusions
– Marc Gravell♦
Nov 20 '18 at 15:15
Maybe, I didn't understand you very well. But I changed it toclass
. I didn't use<Sensor?>
– Дмитрий Суворов
Nov 20 '18 at 16:39
fair enough; changing it toclass
is by far the best solution here
– Marc Gravell♦
Nov 20 '18 at 16:48
Note: it might have worked (re
null
) if you had used <Sensor?>
instead of <Sensor>
- however, I strongly suspect that using struct
for something that represents a data row is probably a bad idea that could cause multiple subtle confusions– Marc Gravell♦
Nov 20 '18 at 15:15
Note: it might have worked (re
null
) if you had used <Sensor?>
instead of <Sensor>
- however, I strongly suspect that using struct
for something that represents a data row is probably a bad idea that could cause multiple subtle confusions– Marc Gravell♦
Nov 20 '18 at 15:15
Maybe, I didn't understand you very well. But I changed it to
class
. I didn't use <Sensor?>
– Дмитрий Суворов
Nov 20 '18 at 16:39
Maybe, I didn't understand you very well. But I changed it to
class
. I didn't use <Sensor?>
– Дмитрий Суворов
Nov 20 '18 at 16:39
fair enough; changing it to
class
is by far the best solution here– Marc Gravell♦
Nov 20 '18 at 16:48
fair enough; changing it to
class
is by far the best solution here– Marc Gravell♦
Nov 20 '18 at 16:48
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53389172%2fdapper-querysingleordefault-doesnt-return-null%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
As you can see in the documentation, if there's no item it will return the default (nothing), if there's one item it will return the item and if there's many items it will return a exception. So it's not returning
null
because it's not supposed to returnnull
.– ikerbera
Nov 20 '18 at 8:47
1
@ikerbera, thanks, I was reading. Вut what is default in my case? I thought default for object is null
– Дмитрий Суворов
Nov 20 '18 at 8:54
What is the value in
result
? You probably have aSensor
class variable with all it's properties at default value. ints at 0, strings at null, bools as false and so on.– ikerbera
Nov 20 '18 at 9:00
@ikerbera I would like use if (result != null) { // do something}
– Дмитрий Суворов
Nov 20 '18 at 9:04
Can you show your
Sensor
class? I'm sure we can find some property that you can check to know if you got no data.– ikerbera
Nov 20 '18 at 9:09