How do I return NULL values after performing a LEFT JOIN in LINQ?Hidden Features of C#?Use of var keyword in C#Performance surprise with “as” and nullable typesLEFT OUTER JOIN in LINQLeft join with LINQ ErrorHow to perform Join between multiple tables in LINQ lambdaLinq to SQL Left Join, Order and Group By CountNhibernate Linq is null on left joinConverting a LINQ Inner join with Left JoinHow To Perform Left Join In Linq With Null Check?
Count rook moves 1D
What would a biological creature need in order to see the future?
Were the women of Travancore, India, taxed for covering their breasts by breast size?
First Number to Contain Each Letter
IEEE Registration Authority mac prefix
What did Boris Johnson mean when he said "extra 34 billion going into the NHS"
'This one' as a pronoun
Are treasury bonds more liquid than USD?
One hour 10 min layover in Newark; International -> Domestic connection. Enough time to clear customs?
What exactly is a softlock?
How does Harry wear the invisibility cloak?
Why would a Intel 8080 chip be destroyed if +12 V is connected before -5 V?
Global variables and information security
Which is the best password hashing algorithm in .NET Core?
Did Alan Turing's student Robin Gandy assert that Charles Babbage had no notion of a universal computing machine?
Splitting polygons at narrowest part using R?
How could it be that the capo isn't changing the pitch?
Round away from zero
How to anonymously report the Establishment Clause being broken?
Did the US Climate Reference Network Show No New Warming Since 2005 in the US?
Is it possible to observe space debris with Binoculars?
Professor refuses to write a recommendation letter
Do 643,000 Americans go bankrupt every year due to medical bills?
How did Gollum know Sauron was gathering the Haradrim to make war?
How do I return NULL values after performing a LEFT JOIN in LINQ?
Hidden Features of C#?Use of var keyword in C#Performance surprise with “as” and nullable typesLEFT OUTER JOIN in LINQLeft join with LINQ ErrorHow to perform Join between multiple tables in LINQ lambdaLinq to SQL Left Join, Order and Group By CountNhibernate Linq is null on left joinConverting a LINQ Inner join with Left JoinHow To Perform Left Join In Linq With Null Check?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I've created a LEFT JOIN in LINQ. How do I get my query to return my nullable values?
+------+------+------+
| c1 | c2 | c3 |
|------+------+------|
| x1 | y1 | z1 |
| x2 | y2 | null |
| x3 | y3 | z1 |
+------+------+------+
Above is basically the SQL table returned after LEFT JOIN as expected
However using LINQ:
var query =
```
into temp
from t in temp.DefaultIfEmpty()
select new
```,
```,
Column3 = (int?)t.ColumnNo
;
Debugger barks at me for Column3
for:
Object reference not set to an instance of an object
c# linq
add a comment |
I've created a LEFT JOIN in LINQ. How do I get my query to return my nullable values?
+------+------+------+
| c1 | c2 | c3 |
|------+------+------|
| x1 | y1 | z1 |
| x2 | y2 | null |
| x3 | y3 | z1 |
+------+------+------+
Above is basically the SQL table returned after LEFT JOIN as expected
However using LINQ:
var query =
```
into temp
from t in temp.DefaultIfEmpty()
select new
```,
```,
Column3 = (int?)t.ColumnNo
;
Debugger barks at me for Column3
for:
Object reference not set to an instance of an object
c# linq
1
t
is null right? So you'd want to do(int?)t.?ColumnNo
?
– Brendan Green
Mar 28 at 3:39
1
It seems to me that you haven't posted valid C# code. Could you give us a minimal reproducible example? That would be your source as valid C# (rather than, or in addition to, the table of data) and then you could provide a query that is valid C#.
– Enigmativity
Mar 28 at 4:14
add a comment |
I've created a LEFT JOIN in LINQ. How do I get my query to return my nullable values?
+------+------+------+
| c1 | c2 | c3 |
|------+------+------|
| x1 | y1 | z1 |
| x2 | y2 | null |
| x3 | y3 | z1 |
+------+------+------+
Above is basically the SQL table returned after LEFT JOIN as expected
However using LINQ:
var query =
```
into temp
from t in temp.DefaultIfEmpty()
select new
```,
```,
Column3 = (int?)t.ColumnNo
;
Debugger barks at me for Column3
for:
Object reference not set to an instance of an object
c# linq
I've created a LEFT JOIN in LINQ. How do I get my query to return my nullable values?
+------+------+------+
| c1 | c2 | c3 |
|------+------+------|
| x1 | y1 | z1 |
| x2 | y2 | null |
| x3 | y3 | z1 |
+------+------+------+
Above is basically the SQL table returned after LEFT JOIN as expected
However using LINQ:
var query =
```
into temp
from t in temp.DefaultIfEmpty()
select new
```,
```,
Column3 = (int?)t.ColumnNo
;
Debugger barks at me for Column3
for:
Object reference not set to an instance of an object
c# linq
c# linq
edited Mar 28 at 3:42
Brendan Green
9,4505 gold badges34 silver badges62 bronze badges
9,4505 gold badges34 silver badges62 bronze badges
asked Mar 28 at 3:19
QmaQma
214 bronze badges
214 bronze badges
1
t
is null right? So you'd want to do(int?)t.?ColumnNo
?
– Brendan Green
Mar 28 at 3:39
1
It seems to me that you haven't posted valid C# code. Could you give us a minimal reproducible example? That would be your source as valid C# (rather than, or in addition to, the table of data) and then you could provide a query that is valid C#.
– Enigmativity
Mar 28 at 4:14
add a comment |
1
t
is null right? So you'd want to do(int?)t.?ColumnNo
?
– Brendan Green
Mar 28 at 3:39
1
It seems to me that you haven't posted valid C# code. Could you give us a minimal reproducible example? That would be your source as valid C# (rather than, or in addition to, the table of data) and then you could provide a query that is valid C#.
– Enigmativity
Mar 28 at 4:14
1
1
t
is null right? So you'd want to do (int?)t.?ColumnNo
?– Brendan Green
Mar 28 at 3:39
t
is null right? So you'd want to do (int?)t.?ColumnNo
?– Brendan Green
Mar 28 at 3:39
1
1
It seems to me that you haven't posted valid C# code. Could you give us a minimal reproducible example? That would be your source as valid C# (rather than, or in addition to, the table of data) and then you could provide a query that is valid C#.
– Enigmativity
Mar 28 at 4:14
It seems to me that you haven't posted valid C# code. Could you give us a minimal reproducible example? That would be your source as valid C# (rather than, or in addition to, the table of data) and then you could provide a query that is valid C#.
– Enigmativity
Mar 28 at 4:14
add a comment |
3 Answers
3
active
oldest
votes
Because of your DefaultIfEmpty()
, the resulting t
can be null. And of course you can't get the ColumnNo
from a null object.
The quick solution would be to use the null conditional operator::
Column3 = t?.ColumnNo; // assuming ColumnNo is already an int? otherwise cast to int?
Which has the same effect as:
Column3 = (t.ColumnNo != null) t.ColumnNo : null;
The null coalescing operator as others suggested won't work:
Column3 = (t.c3 ?? 0)
if t
equals null, you can't get t.c3
, and thus can't check if t.c3
equals null.
Thank you, this worked for me!
– Qma
Mar 28 at 12:22
add a comment |
You can use ??
to set a default value if the column value is null:
select new
```,
```,
Column3 = (t.c3 ?? 0) // if null set its value to zero
;
Isn't the issue thatt
is null, not thatc3
is null?
– Brendan Green
Mar 29 at 2:39
add a comment |
var list = (from PM in DataContext.table1
join U in DataContext.table2 on PM.Id equals U.Id into obj1
from U1 in obj1.DefaultIfEmpty()
select new
Professions = U1.ProfessionName // If null
);
You can use DefaultIfEmpty and use that object to get the column so you can get null values in column.
They are already usingDefaultIfEmpty()
. As with the OP, wouldn'tU1
be null?
– Brendan Green
Mar 29 at 2:39
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%2f55389655%2fhow-do-i-return-null-values-after-performing-a-left-join-in-linq%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Because of your DefaultIfEmpty()
, the resulting t
can be null. And of course you can't get the ColumnNo
from a null object.
The quick solution would be to use the null conditional operator::
Column3 = t?.ColumnNo; // assuming ColumnNo is already an int? otherwise cast to int?
Which has the same effect as:
Column3 = (t.ColumnNo != null) t.ColumnNo : null;
The null coalescing operator as others suggested won't work:
Column3 = (t.c3 ?? 0)
if t
equals null, you can't get t.c3
, and thus can't check if t.c3
equals null.
Thank you, this worked for me!
– Qma
Mar 28 at 12:22
add a comment |
Because of your DefaultIfEmpty()
, the resulting t
can be null. And of course you can't get the ColumnNo
from a null object.
The quick solution would be to use the null conditional operator::
Column3 = t?.ColumnNo; // assuming ColumnNo is already an int? otherwise cast to int?
Which has the same effect as:
Column3 = (t.ColumnNo != null) t.ColumnNo : null;
The null coalescing operator as others suggested won't work:
Column3 = (t.c3 ?? 0)
if t
equals null, you can't get t.c3
, and thus can't check if t.c3
equals null.
Thank you, this worked for me!
– Qma
Mar 28 at 12:22
add a comment |
Because of your DefaultIfEmpty()
, the resulting t
can be null. And of course you can't get the ColumnNo
from a null object.
The quick solution would be to use the null conditional operator::
Column3 = t?.ColumnNo; // assuming ColumnNo is already an int? otherwise cast to int?
Which has the same effect as:
Column3 = (t.ColumnNo != null) t.ColumnNo : null;
The null coalescing operator as others suggested won't work:
Column3 = (t.c3 ?? 0)
if t
equals null, you can't get t.c3
, and thus can't check if t.c3
equals null.
Because of your DefaultIfEmpty()
, the resulting t
can be null. And of course you can't get the ColumnNo
from a null object.
The quick solution would be to use the null conditional operator::
Column3 = t?.ColumnNo; // assuming ColumnNo is already an int? otherwise cast to int?
Which has the same effect as:
Column3 = (t.ColumnNo != null) t.ColumnNo : null;
The null coalescing operator as others suggested won't work:
Column3 = (t.c3 ?? 0)
if t
equals null, you can't get t.c3
, and thus can't check if t.c3
equals null.
answered Mar 28 at 7:59
Harald CoppoolseHarald Coppoolse
14.3k3 gold badges35 silver badges69 bronze badges
14.3k3 gold badges35 silver badges69 bronze badges
Thank you, this worked for me!
– Qma
Mar 28 at 12:22
add a comment |
Thank you, this worked for me!
– Qma
Mar 28 at 12:22
Thank you, this worked for me!
– Qma
Mar 28 at 12:22
Thank you, this worked for me!
– Qma
Mar 28 at 12:22
add a comment |
You can use ??
to set a default value if the column value is null:
select new
```,
```,
Column3 = (t.c3 ?? 0) // if null set its value to zero
;
Isn't the issue thatt
is null, not thatc3
is null?
– Brendan Green
Mar 29 at 2:39
add a comment |
You can use ??
to set a default value if the column value is null:
select new
```,
```,
Column3 = (t.c3 ?? 0) // if null set its value to zero
;
Isn't the issue thatt
is null, not thatc3
is null?
– Brendan Green
Mar 29 at 2:39
add a comment |
You can use ??
to set a default value if the column value is null:
select new
```,
```,
Column3 = (t.c3 ?? 0) // if null set its value to zero
;
You can use ??
to set a default value if the column value is null:
select new
```,
```,
Column3 = (t.c3 ?? 0) // if null set its value to zero
;
answered Mar 28 at 6:16
Ashkan Mobayen KhiabaniAshkan Mobayen Khiabani
24.1k19 gold badges72 silver badges129 bronze badges
24.1k19 gold badges72 silver badges129 bronze badges
Isn't the issue thatt
is null, not thatc3
is null?
– Brendan Green
Mar 29 at 2:39
add a comment |
Isn't the issue thatt
is null, not thatc3
is null?
– Brendan Green
Mar 29 at 2:39
Isn't the issue that
t
is null, not that c3
is null?– Brendan Green
Mar 29 at 2:39
Isn't the issue that
t
is null, not that c3
is null?– Brendan Green
Mar 29 at 2:39
add a comment |
var list = (from PM in DataContext.table1
join U in DataContext.table2 on PM.Id equals U.Id into obj1
from U1 in obj1.DefaultIfEmpty()
select new
Professions = U1.ProfessionName // If null
);
You can use DefaultIfEmpty and use that object to get the column so you can get null values in column.
They are already usingDefaultIfEmpty()
. As with the OP, wouldn'tU1
be null?
– Brendan Green
Mar 29 at 2:39
add a comment |
var list = (from PM in DataContext.table1
join U in DataContext.table2 on PM.Id equals U.Id into obj1
from U1 in obj1.DefaultIfEmpty()
select new
Professions = U1.ProfessionName // If null
);
You can use DefaultIfEmpty and use that object to get the column so you can get null values in column.
They are already usingDefaultIfEmpty()
. As with the OP, wouldn'tU1
be null?
– Brendan Green
Mar 29 at 2:39
add a comment |
var list = (from PM in DataContext.table1
join U in DataContext.table2 on PM.Id equals U.Id into obj1
from U1 in obj1.DefaultIfEmpty()
select new
Professions = U1.ProfessionName // If null
);
You can use DefaultIfEmpty and use that object to get the column so you can get null values in column.
var list = (from PM in DataContext.table1
join U in DataContext.table2 on PM.Id equals U.Id into obj1
from U1 in obj1.DefaultIfEmpty()
select new
Professions = U1.ProfessionName // If null
);
You can use DefaultIfEmpty and use that object to get the column so you can get null values in column.
answered Mar 28 at 8:16
Nirav VasoyaNirav Vasoya
3443 silver badges18 bronze badges
3443 silver badges18 bronze badges
They are already usingDefaultIfEmpty()
. As with the OP, wouldn'tU1
be null?
– Brendan Green
Mar 29 at 2:39
add a comment |
They are already usingDefaultIfEmpty()
. As with the OP, wouldn'tU1
be null?
– Brendan Green
Mar 29 at 2:39
They are already using
DefaultIfEmpty()
. As with the OP, wouldn't U1
be null?– Brendan Green
Mar 29 at 2:39
They are already using
DefaultIfEmpty()
. As with the OP, wouldn't U1
be null?– Brendan Green
Mar 29 at 2:39
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.
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%2f55389655%2fhow-do-i-return-null-values-after-performing-a-left-join-in-linq%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
1
t
is null right? So you'd want to do(int?)t.?ColumnNo
?– Brendan Green
Mar 28 at 3:39
1
It seems to me that you haven't posted valid C# code. Could you give us a minimal reproducible example? That would be your source as valid C# (rather than, or in addition to, the table of data) and then you could provide a query that is valid C#.
– Enigmativity
Mar 28 at 4:14